Added readme and patch to fheroes2

This commit is contained in:
pelya
2010-11-22 18:40:21 +02:00
parent 2915ce7ac6
commit 06828acaea
356 changed files with 170 additions and 105504 deletions

View File

@@ -0,0 +1,165 @@
diff -u -r -b -x .svn -x '*.o' -x '*.d' /home/pelya/src/endless_space/fheroes2/src/fheroes2/army/army.cpp src/fheroes2/army/army.cpp
--- /home/pelya/src/endless_space/fheroes2/src/fheroes2/army/army.cpp 2010-11-15 19:17:40.859414020 +0200
+++ src/fheroes2/army/army.cpp 2010-11-12 16:15:26.000000000 +0200
@@ -728,12 +728,16 @@
void Army::army_t::BattleSetModes(u32 f)
{
- std::for_each(army.begin(), army.end(), std::bind2nd(std::mem_fun_ref(&Troop::BattleSetModes), f));
+ //std::for_each(army.begin(), army.end(), std::bind2nd(std::mem_fun_ref(&Troop::BattleSetModes), f)); // Cannot call non-const function here
+ for( std::vector<Troop> ::iterator it = army.begin(); it != army.end(); it++ )
+ it->BattleSetModes(f);
}
void Army::army_t::BattleResetModes(u32 f)
{
- std::for_each(army.begin(), army.end(), std::bind2nd(std::mem_fun_ref(&Troop::BattleResetModes), f));
+ //std::for_each(army.begin(), army.end(), std::bind2nd(std::mem_fun_ref(&Troop::BattleResetModes), f)); // Cannot call non-const function here
+ for( std::vector<Troop> ::iterator it = army.begin(); it != army.end(); it++ )
+ it->BattleResetModes(f);
}
Army::Troop* Army::army_t::BattleFindModes(u32 f)
diff -u -r -b -x .svn -x '*.o' -x '*.d' /home/pelya/src/endless_space/fheroes2/src/fheroes2/game/fheroes2.cpp src/fheroes2/game/fheroes2.cpp
--- /home/pelya/src/endless_space/fheroes2/src/fheroes2/game/fheroes2.cpp 2010-11-15 19:17:40.859414020 +0200
+++ src/fheroes2/game/fheroes2.cpp 2010-11-22 15:16:37.000000000 +0200
@@ -23,6 +23,7 @@
#include <unistd.h>
#include <iostream>
#include <string>
+#include <SDL.h>
#include "gamedefs.h"
#include "engine.h"
@@ -69,13 +70,18 @@
int main(int argc, char **argv)
{
+ VERBOSE("000 - entered SDL_main()");
Settings & conf = Settings::Get();
int test = 0;
std::cout << "Free Heroes II, " + conf.BuildVersion() << std::endl;
+ VERBOSE("Free Heroes II, " + conf.BuildVersion());
LoadConfigFiles(conf, GetDirname(argv[0]));
+ VERBOSE("001");
+
+#ifndef ANDROID // Crashes for some reason
// getopt
{
int opt;
@@ -109,13 +115,20 @@
default: break;
}
}
+#endif
+
+ VERBOSE("002");
if(conf.SelectVideoDriver().size()) SetVideoDriver(conf.SelectVideoDriver());
+ VERBOSE("003");
+
// random init
Rand::Init();
if(conf.Music()) SetTimidityEnvPath(conf);
+ VERBOSE("004");
+
u32 subsystem = INIT_VIDEO | INIT_TIMER;
if(conf.Sound() || conf.Music())
@@ -128,9 +141,12 @@
Network::SetProtocolVersion(static_cast<u16>((conf.MajorVersion() << 8)) | conf.MinorVersion());
#endif
+ VERBOSE("005");
+
if(SDL::Init(subsystem))
try
{
+ VERBOSE("006");
std::atexit(SDL::Quit);
if(conf.Unicode()) SetLangEnvPath(conf);
@@ -163,17 +179,23 @@
//Ensure the mouse position is updated to prevent bad initial values.
LocalEvent::Get().GetMouseCursor();
+ VERBOSE("007");
+
#ifdef WITH_ZLIB
ZSurface zicons;
if(zicons.Load(FH2_ICONS_WIDTH, FH2_ICONS_HEIGHT, FH2_ICONS_BPP, fh2_icons_pack, FH2_ICONS_SIZE, true)) Display::SetIcons(zicons);
#endif
AGG::Cache & cache = AGG::Cache::Get();
+ VERBOSE("008");
+
// read data dir
if(! cache.ReadDataDir()) Error::Except("FHeroes2: ", "AGG data files not found.");
if(IS_DEBUG(DBG_GAME, DBG_INFO)) conf.Dump();
+ VERBOSE("009");
+
// load palette
cache.LoadPAL();
diff -u -r -b -x .svn -x '*.o' -x '*.d' /home/pelya/src/endless_space/fheroes2/src/fheroes2/system/settings.cpp src/fheroes2/system/settings.cpp
--- /home/pelya/src/endless_space/fheroes2/src/fheroes2/system/settings.cpp 2010-11-15 19:17:34.879413999 +0200
+++ src/fheroes2/system/settings.cpp 2010-11-12 17:31:10.000000000 +0200
@@ -1424,6 +1419,9 @@
{
u32 flags = opt_global.Modes(GLOBAL_USESWSURFACE) ? SDL_SWSURFACE : SDL_SWSURFACE | SDL_HWSURFACE;
if(opt_global.Modes(GLOBAL_FULLSCREEN)) flags |= SDL_FULLSCREEN;
+ #ifdef ANDROID
+ flags = SDL_SWSURFACE;
+ #endif
return flags;
}
diff -u -r -b -x .svn -x '*.o' -x '*.d' /home/pelya/src/endless_space/fheroes2/src/fheroes2/system/settings.h src/fheroes2/system/settings.h
--- /home/pelya/src/endless_space/fheroes2/src/fheroes2/system/settings.h 2010-11-15 19:17:34.869414019 +0200
+++ src/fheroes2/system/settings.h 2010-11-12 16:09:03.000000000 +0200
@@ -27,6 +27,7 @@
#include <list>
#include <iostream>
#include <iomanip>
+#include <sstream>
#include "gamedefs.h"
#include "difficulty.h"
#include "race.h"
@@ -35,6 +36,9 @@
#include "game.h"
#include "game_io.h"
#include "bitmodes.h"
+#ifdef ANDROID
+#include <android/log.h>
+#endif
#define FORMAT_VERSION_2031 0x07EF
#define FORMAT_VERSION_1978 0x07BA
@@ -71,6 +75,9 @@
#ifdef __SYMBIAN32__
#define VERBOSE(x)
#define DEBUG(x, y, z)
+#elif defined(ANDROID)
+#define VERBOSE(x) { std::ostringstream osss; osss << x; __android_log_print(ANDROID_LOG_INFO, "FHeroes", "%s", osss.str().c_str()); }
+#define DEBUG(x, y, z) if(IS_DEBUG((x), (y))) VERBOSE(z)
#else
#define VERBOSE(x) std::cout << x << std::endl
#define DEBUG(x, y, z) if(IS_DEBUG((x), (y))) VERBOSE(z)
diff -u -r -b -x .svn -x '*.o' -x '*.d' /home/pelya/src/endless_space/fheroes2/src/xmlccwrap/gzstream.h src/xmlccwrap/gzstream.h
--- /home/pelya/src/endless_space/fheroes2/src/xmlccwrap/gzstream.h 2010-11-15 19:17:31.869414019 +0200
+++ src/xmlccwrap/gzstream.h 2010-11-12 16:06:58.000000000 +0200
@@ -33,6 +33,7 @@
#include <iostream>
#include <fstream>
#include <zlib.h>
+#include <stdio.h>
#ifdef GZSTREAM_NAMESPACE
namespace GZSTREAM_NAMESPACE {

View File

@@ -1,68 +1,5 @@
Free Heroes2 Engine (is not playable version.)
Prerequisites:
You need to have these libraries (with equivalent devel versions) to build fHeroes2:
- SDL
optional library:
- SDL_mixer (play music: internal midi or external ogg tracks) or build WITHOUT_MIXER
- SDL_image (loading external sprites, create screenshot in png format) or build WITHOUT_IMAGE
- SDL_ttf (unicode support) or build WITHOUT_UNICODE
- SDL_net or build WITHOUT_NETWORK
- libogg
- libpng
- gettext
SDL libraries can be found at http://www.libsdl.org .
Sourcecode you can get it here: http://sourceforge.net/projects/fheroes2/
And translations: http://translations.launchpad.net/fheroes2
Copy origin data/*.agg in to data directory.
Copy maps files (*.mp2) in to maps directory.
Hot keys:
F4 - switch to fullscreen
PrintScreen - create screenshot
Up - move hero top
Down - move hero bottom
Left - move hero left
Right - move hero right
Ctrl + Up - scroll map top (alt. key ';')
Ctrl + Down - scroll map bottom (alt. key '/')
Ctrl + Left - scroll map left (alt. key ',')
Ctrl + Right - scroll map right (alt. key '.')
e - end turn
t - next town
h - next hero
m - move hero
s - save game to fheroes2.sav
l - load game
i - game info
p - puzzle dialog
d - digging artifact for current hero
space or a - default action
return or n - open dialog
o - system dialog
1 - show/hide control panel
2 - show/hide radar (only for option: hide interface = on)
3 - show/hide buttons (only for option: hide interface = on)
4 - show/hide status window (only for option: hide interface = on)
5 - show/hide hero/town icons (only for option: hide interface = on)
Main menu:
n - new game
s - standard game
c - campaign game
m - multi-player game
Battle:
Esc - fast retreat
space - skip turn (soft: heroes3 version)
s - skip turn (hard)
o - options dialog
a - set auto battle
c - show spell book
h - show heroes dialog
Grab Free Heroes 2 sources with SVN from
https://fheroes2.svn.sourceforge.net/svnroot/fheroes2/trunk/fheroes2
and put them here, in directory "fheroes2", then apply patch
fheroes2-r2050.diff
and compile with build.sh

View File

@@ -1,94 +0,0 @@
# makefile
# project: Free Heroes2
#
TARGET := fheroes2
SDL_LIBS := $(shell sdl-config --libs)
SDL_FLAGS := $(shell sdl-config --cflags)
CFLAGS := $(CFLAGS) -Wall -fsigned-char -DWITH_KEYMAPPING
LDFLAGS :=
LIBS :=
ifdef DEBUG
CFLAGS := $(CFLAGS) -O0 -g -pedantic -DWITH_DEBUG
else
CFLAGS := $(CFLAGS) -O2
endif
ifndef WITHOUT_MIXER
CFLAGS := $(CFLAGS) -DWITH_MIXER
SDL_LIBS := $(SDL_LIBS) -lSDL_mixer
endif
ifndef WITHOUT_IMAGE
CFLAGS := $(CFLAGS) -DWITH_IMAGE $(shell libpng12-config --cflags) -DWITH_ZLIB
SDL_LIBS := $(SDL_LIBS) -lSDL_image $(shell libpng12-config --libs) -lz
endif
ifndef WITHOUT_UNICODE
CFLAGS := $(CFLAGS) -DWITH_TTF
SDL_LIBS := $(SDL_LIBS) -lSDL_ttf
endif
ifndef WITHOUT_NETWORK
CFLAGS := $(CFLAGS) -DWITH_NET
SDL_LIBS := $(SDL_LIBS) -lSDL_net
endif
ifndef WITHOUT_XML
CFLAGS := $(CFLAGS) -DWITH_XML
endif
ifndef WITHOUT_ZLIB
CFLAGS := $(CFLAGS) -DWITH_ZLIB
LIBS := $(LIBS) -lz
endif
ifndef WITHOUT_EDITOR
CFLAGS := $(CFLAGS) -DWITH_EDITOR
endif
ifdef RELEASE
CFLAGS := $(CFLAGS) -DBUILD_RELEASE
endif
CFLAGS := $(SDL_FLAGS) $(CFLAGS)
LIBS := $(SDL_LIBS) $(LIBS)
ifeq ($(PLATFORM),)
ifeq ($(OS),Windows_NT)
PLATFORM := mingw
else
PLATFORM := unix
endif
endif
include Makefile.$(PLATFORM)
export CXX AR LINK WINDRES LDFLAGS CFLAGS LIBS PLATFORM
.PHONY: clean
all:
ifndef WITHOUT_XML
$(MAKE) -C xmlccwrap
endif
$(MAKE) -C engine
$(MAKE) -C dist
ifdef WITH_TOOLS
$(MAKE) -C tools
endif
ifndef WITHOUT_UNICODE
$(MAKE) -C dist pot
endif
clean:
ifndef WITHOUT_XML
$(MAKE) -C xmlccwrap clean
endif
ifdef WITH_TOOLS
$(MAKE) -C tools clean
endif
$(MAKE) -C dist clean
$(MAKE) -C engine clean

View File

@@ -1,5 +0,0 @@
include Makefile.unix
ifndef WITHOUT_UNICODE
LIBS := $(LIBS) -lintl -liconv
endif

View File

@@ -1,27 +0,0 @@
ifndef WITHOUT_MIXER
LIBS := $(LIBS) -lSDL -lmad -lvorbisfile -lvorbis -logg
endif
ifndef WITHOUT_IMAGE
LIBS := $(LIBS) -lSDL -lpng -ljpeg
endif
ifndef WITHOUT_UNICODE
LIBS := $(LIBS) -lintl -lfreetype
endif
ifndef WITHOUT_AUDIOCD
CFLAGS := $(CFLAGS) -DWITH_AUDIOCD
endif
ifdef WITH_ICONS
IDICON := 1099
CFLAGS := $(CFLAGS) -DID_ICON=$(IDICON)
export IDICON
endif
AR := i686-pc-mingw32-ar
CXX := i686-pc-mingw32-g++
WINDRES := i686-pc-mingw32-windres
CFLAGS := $(CFLAGS) -O2 -static
LIBS := -static -Wl,-Bstatic $(LIBS) -lwinmm

View File

@@ -1,20 +0,0 @@
ifndef WITHOUT_MIXER
LIBS := $(LIBS) -lSDL -lmad -lvorbisfile -lvorbis -logg
endif
ifndef WITHOUT_UNICODE
LIBS := $(LIBS) -lintl -lfreetype
endif
ifdef WITH_ICONS
IDICON := 1099
CFLAGS := $(CFLAGS) -DID_ICON=$(IDICON)
export IDICON
endif
AR := arm-mingw32ce-ar
CXX := arm-mingw32ce-g++
WINDRES := arm-mingw32ce-windres
CFLAGS := $(CFLAGS) -ffunction-sections -DWITHOUT_MOUSE -Os -static
LIBS := -Wl,-Bstatic $(LIBS) -lmmtimer
LDFLAGS := -static $(LDFLAGS) -Wl,--gc-sections

View File

@@ -1,16 +0,0 @@
AR := ar
CXX := g++
LINK := libtool
SIMPLE_LINK_FLAGS := -dynamic -undefined suppress -flat_namespace
ifndef WITHOUT_UNICODE
LIBS := $(LIBS) -lintl -liconv
endif
# the config scripts generate link flags like -Wl,-framework which screws up
# libtool on OS X, so we remove the -Wl, prefix and everything works fine.
search_pattern := -Wl
comma_pattern := ,
LINK_FLAGS := $(subst $(search_pattern),,$(LIBS))
LINK_FLAGS := $(subst $(comma_pattern), ,$(LINK_FLAGS)) $(SIMPLE_LINK_FLAGS)

View File

@@ -1,2 +0,0 @@
AR := ar
CXX := g++

View File

@@ -1,280 +0,0 @@
/*
Based on zlib license - see http://www.gzip.org/zlib/zlib_license.html
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
"Philip D. Bober" <wildfire1138@mchsi.com>
*/
/* 2010 - support: sdl 1.3 - fheroes2 team */
/* 2009 - changed: save 16bpp, 24bpp - fheroes2 team */
/* 2008 - changed: default color - fheroes2 team */
/**
* 4/17/04 - IMG_SavePNG & IMG_SavePNG_RW - Philip D. Bober
* 11/08/2004 - Compr fix, levels -1,1-7 now work - Tyler Montbriand
*/
#include <stdlib.h>
#include "SDL.h"
#include "IMG_savepng.h"
#ifdef WITH_IMAGE
#include "png.h"
int IMG_SavePNG(const char *file, SDL_Surface *surf,int compression){
SDL_RWops *fp;
int ret;
fp=SDL_RWFromFile(file,"wb");
if( fp == NULL ) {
return (-1);
}
ret=IMG_SavePNG_RW(fp,surf,compression);
SDL_RWclose(fp);
return ret;
}
static void png_write_data(png_structp png_ptr,png_bytep data, png_size_t length){
SDL_RWops *rp = (SDL_RWops*) png_get_io_ptr(png_ptr);
SDL_RWwrite(rp,data,1,length);
}
int IMG_SavePNG_RW(SDL_RWops *src, SDL_Surface *surf,int compression){
png_structp png_ptr;
png_infop info_ptr;
SDL_PixelFormat *fmt=NULL;
SDL_Surface *tempsurf=NULL;
int ret,funky_format;
unsigned int i;
Uint8 used_alpha, temp_alpha = 0;
png_colorp palette;
Uint8 *palette_alpha=NULL;
png_byte **row_pointers=NULL;
png_ptr=NULL;info_ptr=NULL;palette=NULL;ret=-1;
funky_format=0;
if( !src || !surf) {
goto savedone; /* Nothing to do. */
}
row_pointers=(png_byte **)malloc(surf->h * sizeof(png_byte*));
if (!row_pointers) {
SDL_SetError("Couldn't allocate memory for rowpointers");
goto savedone;
}
png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,NULL,NULL);
if (!png_ptr){
SDL_SetError("Couldn't allocate memory for PNG file");
goto savedone;
}
info_ptr= png_create_info_struct(png_ptr);
if (!info_ptr){
SDL_SetError("Couldn't allocate image information for PNG file");
goto savedone;
}
/* setup custom writer functions */
png_set_write_fn(png_ptr,(voidp)src,png_write_data,NULL);
if (setjmp(png_jmpbuf(png_ptr))){
SDL_SetError("Unknown error writing PNG");
goto savedone;
}
if(compression>Z_BEST_COMPRESSION)
compression=Z_BEST_COMPRESSION;
if(compression == Z_NO_COMPRESSION) // No compression
{
png_set_filter(png_ptr,0,PNG_FILTER_NONE);
png_set_compression_level(png_ptr,Z_NO_COMPRESSION);
}
else if(compression<0) // Default compression
png_set_compression_level(png_ptr,Z_DEFAULT_COMPRESSION);
else
png_set_compression_level(png_ptr,compression);
fmt=surf->format;
if(fmt->BitsPerPixel==8){ /* Paletted */
png_set_IHDR(png_ptr,info_ptr,
surf->w,surf->h,8,PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
palette=(png_colorp) malloc(fmt->palette->ncolors * sizeof(png_color));
if (!palette) {
SDL_SetError("Couldn't create memory for palette");
goto savedone;
}
for (i=0;(signed int)i<fmt->palette->ncolors;i++) {
palette[i].red=fmt->palette->colors[i].r;
palette[i].green=fmt->palette->colors[i].g;
palette[i].blue=fmt->palette->colors[i].b;
}
png_set_PLTE(png_ptr,info_ptr,palette,fmt->palette->ncolors);
if (surf->flags&SDL_SRCCOLORKEY) {
Uint32 colorkey = 0;
#if SDL_VERSION_ATLEAST(1, 3, 0)
SDL_GetColorKey(surf, &colorkey);
#else
colorkey = fmt->colorkey + 1;
#endif
palette_alpha=(Uint8 *)malloc((colorkey+1)*sizeof(Uint8));
if (!palette_alpha) {
SDL_SetError("Couldn't create memory for palette transparency");
goto savedone;
}
memset(palette_alpha, 0, (colorkey+1)*sizeof(Uint8));
palette_alpha[colorkey]=0;
png_set_tRNS(png_ptr,info_ptr,palette_alpha,(colorkey+1),NULL);
}
}else{ /* Truecolor */
png_set_IHDR(png_ptr,info_ptr,
surf->w,surf->h,8,PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
}
png_write_info(png_ptr, info_ptr);
if (fmt->BitsPerPixel==8) { /* Paletted */
for(i=0;(signed int)i<surf->h;i++){
row_pointers[i]= ((png_byte*)surf->pixels) + i*surf->pitch;
}
if(SDL_MUSTLOCK(surf)){
SDL_LockSurface(surf);
}
png_write_image(png_ptr, row_pointers);
if(SDL_MUSTLOCK(surf)){
SDL_UnlockSurface(surf);
}
}else{ /* Truecolor */
if(fmt->BytesPerPixel==3){
if(fmt->Amask){ /* check for 24 bit with alpha */
funky_format=1;
}else{
/* Check for RGB/BGR/GBR/RBG/etc surfaces.*/
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
if(fmt->Rmask!=0xFF0000
|| fmt->Gmask!=0x00FF00
|| fmt->Bmask!=0x0000FF){
#else
if(fmt->Rmask!=0x0000FF
|| fmt->Gmask!=0x00FF00
|| fmt->Bmask!=0xFF0000){
#endif
funky_format=1;
}
}
}else if (fmt->BytesPerPixel==4){
if (!fmt->Amask) { /* check for 32bit but no alpha */
funky_format=1;
}else{
/* Check for ARGB/ABGR/GBAR/RABG/etc surfaces.*/
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
if(fmt->Rmask!=0xFF000000
|| fmt->Gmask!=0x00FF0000
|| fmt->Bmask!=0x0000FF00
|| fmt->Amask!=0x000000FF){
#else
if(fmt->Rmask!=0x000000FF
|| fmt->Gmask!=0x0000FF00
|| fmt->Bmask!=0x00FF0000
|| fmt->Amask!=0xFF000000){
#endif
funky_format=1;
}
}
}else{ /* 555 or 565 16 bit color */
funky_format=1;
}
if (funky_format) {
/* Allocate non-funky format, and copy pixeldata in*/
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
tempsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 32,
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
#else
tempsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 32,
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
#endif
if(!tempsurf){
SDL_SetError("Couldn't allocate temp surface");
goto savedone;
}
if(surf->flags&SDL_SRCALPHA){
#if SDL_VERSION_ATLEAST(1, 3, 0)
SDL_GetSurfaceAlphaMod(surf, &temp_alpha);
#else
temp_alpha=fmt->alpha;
#endif
used_alpha=1;
SDL_SetAlpha(surf,0,255); /* Set for an opaque blit */
}else{
used_alpha=0;
}
if(SDL_BlitSurface(surf,NULL,tempsurf,NULL)!=0){
SDL_SetError("Couldn't blit surface to temp surface");
SDL_FreeSurface(tempsurf);
goto savedone;
}
if (used_alpha) {
SDL_SetAlpha(surf,SDL_SRCALPHA,(Uint8)temp_alpha); /* Restore alpha settings*/
}
for(i=0;(signed int)i<tempsurf->h;i++){
row_pointers[i]= ((png_byte*)tempsurf->pixels) + i*tempsurf->pitch;
}
if(SDL_MUSTLOCK(tempsurf)){
SDL_LockSurface(tempsurf);
}
png_write_image(png_ptr, row_pointers);
if(SDL_MUSTLOCK(tempsurf)){
SDL_UnlockSurface(tempsurf);
}
SDL_FreeSurface(tempsurf);
} else {
for(i=0;(signed int)i<surf->h;i++){
row_pointers[i]= ((png_byte*)surf->pixels) + i*surf->pitch;
}
if(SDL_MUSTLOCK(surf)){
SDL_LockSurface(surf);
}
png_write_image(png_ptr, row_pointers);
if(SDL_MUSTLOCK(surf)){
SDL_UnlockSurface(surf);
}
}
}
png_write_end(png_ptr, NULL);
ret=0; /* got here, so nothing went wrong. YAY! */
savedone: /* clean up and return */
png_destroy_write_struct(&png_ptr,&info_ptr);
if (palette) {
free(palette);
}
if (palette_alpha) {
free(palette_alpha);
}
if (row_pointers) {
free(row_pointers);
}
return ret;
}
#endif

View File

@@ -1,56 +0,0 @@
/*
Based on zlib license - see http://www.gzip.org/zlib/zlib_license.html
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
"Philip D. Bober" <wildfire1138@mchsi.com>
*/
#ifndef __IMG_SAVETOPNG_H__
#define __IMG_SAVETOPNG_H__
/* #include <SDL/begin_code.h> */
#ifdef __cplusplus
extern "C" {
#endif
#define IMG_COMPRESS_OFF 0
#define IMG_COMPRESS_MAX 9
#define IMG_COMPRESS_DEFAULT -1
#ifdef WITH_IMAGE
/**
* Takes a filename, a surface to save, and a compression level. The
* compression level can be 0(min) through 9(max), or -1(default).
*/
DECLSPEC int SDLCALL IMG_SavePNG(const char *file,
SDL_Surface *surf,
int compression);
/**
* Takes a SDL_RWops pointer, a surface to save, and a compression level.
* compression can be 0(min) through 9(max), or -1(default).
*/
DECLSPEC int SDLCALL IMG_SavePNG_RW(SDL_RWops *src,
SDL_Surface *surf,
int compression);
#endif
#ifdef __cplusplus
}
#endif
#endif/*__IMG_SAVETOPNG_H__*/

View File

@@ -1,20 +0,0 @@
# makefile
# project: Free Heroes2
# libSDL C++ wrapper engine
TARGET := libengine
all: $(TARGET).a
$(TARGET).a: $(patsubst %.cpp, %.o, $(wildcard *.cpp))
$(AR) crvs $@ $^
%.o: %.cpp
$(CXX) -c -MD $< $(CFLAGS)
include $(wildcard *.d)
.PHONY: clean
clean:
rm -f *.a *.so *.d *.o

View File

@@ -1,86 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include "audio.h"
namespace Audio
{
static Spec hardware;
}
Audio::Spec::Spec()
{
freq = 0;
format = 0;
channels = 0;
silence = 0;
samples = 0;
size = 0;
callback = NULL;
userdata = NULL;
}
Audio::CVT::CVT()
{
needed = 0;
src_format = 0;
dst_format = 0;
rate_incr = 0;
buf = NULL;
len = 0;
len_cvt = 0;
len_mult = 0;
len_ratio = 0;
filters[0] = NULL;
filters[1] = NULL;
filters[2] = NULL;
filters[3] = NULL;
filters[4] = NULL;
filters[5] = NULL;
filters[6] = NULL;
filters[7] = NULL;
filters[8] = NULL;
filters[9] = NULL;
filter_index = 0;
}
bool Audio::CVT::Build(const Audio::Spec & src, const Audio::Spec & dst)
{
if(1 == SDL_BuildAudioCVT(this, src.format, src.channels, src.freq, dst.format, dst.channels, dst.freq)) return true;
std::cerr << "Audio::CVT::Build: " << SDL_GetError() << std::endl;
return false;
}
bool Audio::CVT::Convert(void)
{
if(0 == SDL_ConvertAudio(this)) return true;
std::cerr << "Audio::CVT::Convert: " << SDL_GetError() << std::endl;
return false;
}
Audio::Spec & Audio::GetHardwareSpec(void)
{
return hardware;
}

View File

@@ -1,46 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2AUDIO_H
#define H2AUDIO_H
#include "types.h"
namespace Audio
{
struct Spec : public SDL_AudioSpec
{
Spec();
};
struct CVT : public SDL_AudioCVT
{
CVT();
bool Build(const Spec & src, const Spec & dst);
bool Convert(void);
};
Spec & GetHardwareSpec(void);
}
#endif

View File

@@ -1,135 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Josh Matthews <josh@joshmatthews.net> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef WITH_AUDIOCD
#include <iostream>
#include "audio_mixer.h"
#include "audio_cdrom.h"
namespace Cdrom
{
void Open(void);
void Close(void);
static SDL_CD *cd = NULL;
static int currentTrack = -1;
static unsigned int startTime = 0;
static unsigned int tickLength;
static SDL_Thread *loopThread;
static SDL_mutex *cdLock;
int LoopCheck(void *data);
}
void Cdrom::Open(void)
{
for(int i = 0; i < SDL_CDNumDrives(); i++)
{
SDL_CD *drive = SDL_CDOpen(i);
if(drive)
{
if(!CD_INDRIVE(SDL_CDStatus(drive)))
{
SDL_CDClose(drive);
continue;
}
else
if(drive->numtracks > 1 && drive->track[0].type == SDL_DATA_TRACK)
{
cd = drive;
break;
}
}
}
if(cd)
{
loopThread = SDL_CreateThread(&LoopCheck, NULL);
cdLock = SDL_CreateMutex();
}
std::cerr << "Cdrom::Open: " << (cd ? "found CD audio device." : "no CDROM devices available.") << std::endl;
}
void Cdrom::Close(void)
{
if(cd)
{
SDL_CDStop(cd);
SDL_KillThread(loopThread);
SDL_DestroyMutex(cdLock);
SDL_CDClose(cd);
cd = NULL;
}
}
bool Cdrom::isValid(void)
{
return cd;
}
int Cdrom::LoopCheck(void *data)
{
while(1)
{
SDL_Delay(5000);
SDL_LockMutex(cdLock);
if(startTime && SDL_GetTicks() - startTime > tickLength)
Play(currentTrack, true, true);
SDL_UnlockMutex(cdLock);
}
return 0;
}
void Cdrom::Play(const u8 track, bool loop, bool force)
{
if(Mixer::isValid() && cd)
{
SDL_LockMutex(cdLock);
if(currentTrack != track || force)
{
if(SDL_CDPlayTracks(cd, track, 0, 1, 0) < 0)
std::cerr << "Cdrom::Play: Couldn't play track " << static_cast<int>(track) << std::endl;
currentTrack = track;
if(loop)
{
tickLength = (unsigned int)((cd->track[track].length / CD_FPS) * 0.01f);
startTime = SDL_GetTicks();
}
else startTime = 0;
if(SDL_CDStatus(cd) != CD_PLAYING)
std::cerr << "Cdrom::Play: CD is not playing" << SDL_GetError() << std::endl;
}
SDL_UnlockMutex(cdLock);
}
}
void Cdrom::Pause(void)
{
if(cd) SDL_CDPause(cd);
}
#endif

View File

@@ -1,37 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Josh Matthews <josh@joshmatthews.net> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2AUDIO_CDROM_H
#define H2AUDIO_CDROM_H
#ifdef WITH_AUDIOCD
#include "types.h"
namespace Cdrom
{
bool isValid(void);
void Play(const u8 track, bool loop, bool force = false);
void Pause(void);
}
#endif
#endif

View File

@@ -1,490 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <algorithm>
#include <iostream>
#include "engine.h"
#include "audio.h"
#include "audio_cdrom.h"
#include "audio_music.h"
#include "audio_mixer.h"
namespace Mixer
{
void Init(void);
void Quit(void);
bool valid = false;
}
bool Mixer::isValid(void)
{
return valid;
}
#ifdef WITH_MIXER
#include "SDL_mixer.h"
void FreeChannel(int channel)
{
Mixer::chunk_t* sample = Mix_GetChunk(channel);
if(sample) Mix_FreeChunk(sample);
}
void Mixer::Init(void)
{
if(SDL::SubSystem(SDL_INIT_AUDIO))
{
Audio::Spec & hardware = Audio::GetHardwareSpec();
hardware.freq = 22050;
hardware.format = AUDIO_S16;
hardware.channels = 2;
hardware.samples = 2048;
if(0 != Mix_OpenAudio(hardware.freq, hardware.format, hardware.channels, hardware.samples))
{
std::cerr << "Mixer: " << SDL_GetError() << std::endl;
valid = false;
}
else
{
int channels = 0;
Mix_QuerySpec(&hardware.freq, &hardware.format, &channels);
hardware.channels = channels;
valid = true;
}
}
else
{
std::cerr << "Mixer: audio subsystem not initialize" << std::endl;
valid = false;
}
}
void Mixer::Quit(void)
{
if(! SDL::SubSystem(SDL_INIT_AUDIO) || !valid) return;
Mixer::Reset();
valid = false;
Mix_CloseAudio();
}
void Mixer::SetChannels(u8 num)
{
Mix_AllocateChannels(num);
Mix_ReserveChannels(1);
}
void Mixer::FreeChunk(chunk_t *sample)
{
if(sample) Mix_FreeChunk(sample);
}
Mixer::chunk_t* Mixer::LoadWAV(const char* file)
{
Mix_Chunk *sample = Mix_LoadWAV(file);
if(!sample) std::cerr << "Mixer::LoadWAV: " << Mix_GetError() << std::endl;
return sample;
}
Mixer::chunk_t* Mixer::LoadWAV(const u8* ptr, u32 size)
{
Mix_Chunk *sample = Mix_LoadWAV_RW(SDL_RWFromConstMem(ptr, size), 1);
if(!sample) std::cerr << "Mixer::LoadWAV: "<< Mix_GetError() << std::endl;
return sample;
}
int Mixer::Play(chunk_t* sample, int channel, bool loop)
{
int res = Mix_PlayChannel(channel, sample, loop ? -1 : 0);
if(res == -1) std::cerr << "Mixer::Play: " << Mix_GetError() << std::endl;;
return res;
}
int Mixer::Play(const char* file, int channel, bool loop)
{
if(valid)
{
chunk_t* sample = LoadWAV(file);
if(sample)
{
Mix_ChannelFinished(FreeChannel);
return Play(sample, channel, loop);
}
}
return -1;
}
int Mixer::Play(const u8* ptr, u32 size, int channel, bool loop)
{
if(valid && ptr)
{
chunk_t* sample = LoadWAV(ptr, size);
if(sample)
{
Mix_ChannelFinished(FreeChannel);
return Play(sample, channel, loop);
}
}
return -1;
}
u16 Mixer::MaxVolume(void)
{
return MIX_MAX_VOLUME;
}
u16 Mixer::Volume(int channel, s16 vol)
{
if(!valid) return 0;
return Mix_Volume(channel, vol > MIX_MAX_VOLUME ? MIX_MAX_VOLUME : vol);
}
void Mixer::Pause(int channel)
{
Mix_Pause(channel);
}
void Mixer::Resume(int channel)
{
Mix_Resume(channel);
}
void Mixer::Stop(int channel)
{
Mix_HaltChannel(channel);
}
void Mixer::Reset(void)
{
Music::Reset();
#ifdef WITH_AUDIOCD
if(Cdrom::isValid()) Cdrom::Pause();
#endif
Mix_HaltChannel(-1);
}
u8 Mixer::isPlaying(int channel)
{
return Mix_Playing(channel);
}
u8 Mixer::isPaused(int channel)
{
return Mix_Paused(channel);
}
void Mixer::Reduce(void)
{
}
void Mixer::Enhance(void)
{
}
#else
enum { MIX_PLAY = 0x01, MIX_LOOP = 0x02, MIX_REDUCE = 0x04, MIX_ENHANCE = 0x08 };
struct chunk_t
{
chunk_t() : data(NULL), length(0), position(0), volume1(0), state(0) {};
bool this_ptr(const chunk_t* ch) const{ return ch == this; };
const u8 * data;
u32 length;
u32 position;
s16 volume1;
s16 volume2;
u8 state;
};
namespace Mixer
{
bool PredicateIsFreeSound(const chunk_t &);
void PredicateStopSound(chunk_t &);
void PredicateStartSound(chunk_t &);
void AudioCallBack(void*, u8*, int);
std::vector<chunk_t> chunks;
u8 reserved_channels;
}
void Mixer::PredicateStopSound(chunk_t & ch)
{
SDL_LockAudio();
ch.state &= ~MIX_PLAY;
SDL_UnlockAudio();
}
void Mixer::PredicateStartSound(chunk_t & ch)
{
SDL_LockAudio();
ch.state |= MIX_PLAY;
SDL_UnlockAudio();
}
bool Mixer::PredicateIsFreeSound(const chunk_t & ch)
{
return !(ch.state & MIX_PLAY);
}
void Mixer::AudioCallBack(void *unused, u8 *stream, int length)
{
for(u8 ii = 0; ii < chunks.size(); ++ii)
{
chunk_t & ch = chunks[ii];
if((ch.state & MIX_PLAY) && ch.volume1)
{
if(ch.state & MIX_REDUCE)
{
ch.volume1 -= 10;
if(ch.volume1 <= 0)
{
ch.volume1 = 0;
ch.state &= ~MIX_REDUCE;
}
}
else
if(ch.state & MIX_ENHANCE)
{
ch.volume1 += 10;
if(ch.volume1 >= ch.volume2)
{
ch.volume1 = ch.volume2;
ch.state &= ~MIX_ENHANCE;
}
}
SDL_MixAudio(stream, &ch.data[ch.position], (ch.position + length > ch.length ? ch.length - ch.position : length), ch.volume1);
ch.position += length;
if(ch.position >= ch.length)
{
ch.position = 0;
if(!(ch.state & MIX_LOOP)) ch.state &= ~MIX_PLAY;
}
}
}
}
void Mixer::Init(void)
{
if(SDL::SubSystem(SDL_INIT_AUDIO))
{
Audio::Spec spec;
spec.freq = 22050;
spec.format = AUDIO_S16;
spec.channels = 2;
spec.samples = 2048;
spec.callback = AudioCallBack;
if(0 > SDL_OpenAudio(&spec, &Audio::GetHardwareSpec()))
{
std::cerr << "Mixer::Init: " << SDL_GetError() << std::endl;
valid = false;
}
else
{
SDL_PauseAudio(0);
valid = true;
reserved_channels = 0;
}
}
else
{
std::cerr << "Mixer::Init: audio subsystem not initialize" << std::endl;
valid = false;
}
}
void Mixer::Quit(void)
{
if(! SDL::SubSystem(SDL_INIT_AUDIO) || !valid) return;
Music::Reset();
Mixer::Reset();
SDL_CloseAudio();
chunks.clear();
valid = false;
}
void Mixer::SetChannels(u8 num)
{
chunks.resize(num);
reserved_channels = 1;
}
u16 Mixer::MaxVolume(void)
{
return SDL_MIX_MAXVOLUME;
}
u16 Mixer::Volume(int ch, s16 vol)
{
if(!valid) return 0;
if(vol > SDL_MIX_MAXVOLUME) vol = SDL_MIX_MAXVOLUME;
if(ch < 0)
{
for(u8 ii = 0; ii < chunks.size(); ++ii)
{
SDL_LockAudio();
chunks[ii].volume1 = vol;
chunks[ii].volume2 = vol;
SDL_UnlockAudio();
}
}
else
if(ch < static_cast<int>(chunks.size()))
{
if(0 > vol)
{
vol = chunks[ch].volume1;
}
else
{
SDL_LockAudio();
chunks[ch].volume1 = vol;
chunks[ch].volume2 = vol;
SDL_UnlockAudio();
}
}
return vol;
}
int Mixer::Play(const u8* ptr, u32 size, int channel, bool loop)
{
if(valid && ptr)
{
chunk_t* ch = NULL;
if(0 > channel)
{
std::vector<chunk_t>::iterator it = std::find_if(chunks.begin(), chunks.end(), std::bind2nd(std::mem_fun_ref(&chunk_t::this_ptr), ptr));
if(it == chunks.end())
{
it = std::find_if(chunks.begin() + reserved_channels, chunks.end(), PredicateIsFreeSound);
if(it == chunks.end())
{
std::cerr << "Mixer::PlayRAW: mixer is full" << std::endl;
return -1;
}
}
ch = &(*it);
channel = it - chunks.begin();
}
else
if(channel < static_cast<int>(chunks.size()))
ch = &chunks[channel];
if(ch)
{
SDL_LockAudio();
ch->state |= (loop ? MIX_LOOP | MIX_PLAY : MIX_PLAY);
ch->data = ptr;
ch->length = size;
ch->position = 0;
SDL_UnlockAudio();
}
return channel;
}
return -1;
}
void Mixer::Pause(int ch)
{
if(! valid) return;
if(0 > ch)
std::for_each(chunks.begin(), chunks.end(), PredicateStopSound);
else
if(ch < static_cast<int>(chunks.size())) PredicateStopSound(chunks[ch]);
}
void Mixer::Resume(int ch)
{
if(! valid) return;
if(0 > ch)
std::for_each(chunks.begin(), chunks.end(), PredicateStartSound);
else
if(ch < static_cast<int>(chunks.size())) PredicateStartSound(chunks[ch]);
}
u8 Mixer::isPlaying(int ch)
{
return 0 <= ch && ch < static_cast<int>(chunks.size()) && (chunks[ch].state & MIX_PLAY);
}
u8 Mixer::isPaused(int ch)
{
return 0 <= ch && ch < static_cast<int>(chunks.size()) && !(chunks[ch].state & MIX_PLAY);
}
void Mixer::Stop(int ch)
{
Pause(ch);
}
void Mixer::Reset(void)
{
Music::Reset();
#ifdef WITH_AUDIOCD
if(Cdrom::isValid()) Cdrom::Pause();
#endif
Pause(-1);
}
void Mixer::Reduce(void)
{
if(! valid) return;
std::vector<chunk_t>::iterator it = chunks.begin();
for(; it != chunks.end(); ++it)
{
if((*it).state & MIX_PLAY)
{
SDL_LockAudio();
(*it).state |= MIX_REDUCE;
SDL_UnlockAudio();
}
}
}
void Mixer::Enhance(void)
{
if(! valid) return;
std::vector<chunk_t>::iterator it = chunks.begin();
for(; it != chunks.end(); ++it)
{
if((*it).state & MIX_PLAY)
{
SDL_LockAudio();
(*it).state |= MIX_ENHANCE;
SDL_UnlockAudio();
}
}
}
#endif

View File

@@ -1,64 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2AUDIO_MIXER_H
#define H2AUDIO_MIXER_H
#include <vector>
#include "types.h"
#ifdef WITH_MIXER
#include "SDL_mixer.h"
#endif
namespace Mixer
{
#ifdef WITH_MIXER
typedef Mix_Chunk chunk_t;
void FreeChunk(chunk_t*);
chunk_t* LoadWAV(const char*);
chunk_t* LoadWAV(const u8*, u32);
int Play(chunk_t*, int, bool);
int Play(const char*, int = -1, bool = false);
#endif
int Play(const u8*, u32, int = -1, bool = false);
void SetChannels(u8);
u16 MaxVolume(void);
u16 Volume(int ch, s16 = -1);
void Pause(int ch = -1);
void Resume(int ch = -1);
void Stop(int ch = -1);
void Reset(void);
u8 isPlaying(int);
u8 isPaused(int);
bool isValid(void);
void Reduce(void);
void Enhance(void);
}
#endif

View File

@@ -1,213 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include "audio_mixer.h"
#include "audio_music.h"
#ifdef WITH_MIXER
#include "SDL_mixer.h"
namespace Music
{
void Play(bool);
static Mix_Music * music = NULL;
static u16 fadein = 0;
static u16 fadeout = 0;
}
void Music::Play(bool loop)
{
if(fadein)
{
if(music && Mix_FadeInMusic(music, loop ? -1 : 0, fadein) == -1)
std::cerr << "Music::Play: " << Mix_GetError() << std::endl;
}
else
{
if(music && Mix_PlayMusic(music, loop ? -1 : 0) == -1)
std::cerr << "Music::Play: " << Mix_GetError() << std::endl;
}
}
void Music::Play(const u8* ptr, u32 size, bool loop)
{
if(! Mixer::isValid()) return;
if(ptr && size)
{
Reset();
SDL_RWops *rwops = SDL_RWFromConstMem(ptr, size);
music = Mix_LoadMUS_RW(rwops);
SDL_FreeRW(rwops);
Music::Play(loop);
}
}
void Music::Play(const char* file, bool loop)
{
if(! Mixer::isValid()) return;
Reset();
music = Mix_LoadMUS(file);
if(! music)
std::cerr << "Music::Play: " << Mix_GetError() << std::endl;
else
Music::Play(loop);
}
void Music::SetFadeIn(u16 f)
{
fadein = f;
}
void Music::SetFadeOut(u16 f)
{
fadeout = f;
}
u16 Music::Volume(s16 vol)
{
return Mixer::isValid() ? (Mix_VolumeMusic(vol > MIX_MAX_VOLUME ? MIX_MAX_VOLUME : vol)) : 0;
}
void Music::Pause(void)
{
if(! Mixer::isValid() && music) Mix_PauseMusic();
}
void Music::Resume(void)
{
if(Mixer::isValid() && music) Mix_ResumeMusic();
}
void Music::Reset(void)
{
if(Mixer::isValid() && music)
{
if(fadeout)
while(!Mix_FadeOutMusic(fadeout) && Mix_PlayingMusic()) SDL_Delay(50);
else
Mix_HaltMusic();
Mix_FreeMusic(music);
music = NULL;
}
}
bool Music::isPlaying(void)
{
return Mixer::isValid() && Mix_PlayingMusic();
}
bool Music::isPaused(void)
{
return Mixer::isValid() && Mix_PausedMusic();
}
#else
#include "thread.h"
struct info_t
{
info_t() : loop(false){};
std::string run;
bool loop;
};
namespace Music
{
SDL::Thread music;
info_t info;
}
int callbackPlayMusic(void *ptr)
{
if(ptr && system(NULL))
{
info_t & info = *reinterpret_cast<info_t *>(ptr);
if(info.loop)
{
while(1){ system(info.run.c_str()); DELAY(10); }
}
else
return system(info.run.c_str());
}
return -1;
}
void Music::Play(const u8* ptr, u32 size, bool loop)
{
}
void Music::Play(const char* run, bool loop)
{
if(music.IsRun())
{
if(info.run == run) return;
music.Kill();
}
info.run = run;
info.loop = loop;
music.Create(callbackPlayMusic, &info);
}
void Music::SetFadeIn(u16 f)
{
}
void Music::SetFadeOut(u16 f)
{
}
u16 Music::Volume(s16 vol)
{
return 0;
}
void Music::Pause(void)
{
}
void Music::Resume(void)
{
}
bool Music::isPlaying(void)
{
return music.IsRun();
}
bool Music::isPaused(void)
{
return false;
}
void Music::Reset(void)
{
if(music.IsRun()) music.Kill();
}
#endif

View File

@@ -1,42 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2AUDIO_MUSIC_H
#define H2AUDIO_MUSIC_H
#include "types.h"
namespace Music
{
void Play(const u8* ptr, u32 size, bool loop);
void Play(const char* file, bool loop);
u16 Volume(s16 vol);
void SetFadeIn(u16);
void SetFadeOut(u16);
void Pause(void);
void Resume(void);
void Reset(void);
bool isPlaying(void);
bool isPaused(void);
}
#endif

View File

@@ -1,106 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "background.h"
#include "display.h"
Background::Background(const Rect &rt) : Surface(), Rect(rt)
{
}
Background::Background(s16 x, s16 y, u16 w, u16 h) : Surface(), Rect(x, y, w, h)
{
}
Background::Background(const Point &pt, u16 w, u16 h) : Surface(), Rect(pt, w, h)
{
}
bool Background::isValid(void) const
{
return Surface::isValid();
}
void Background::Save(void)
{
// resize background
if(Surface::isValid() && (Size::w != Surface::w() || Size::h != Surface::h())) FreeSurface(*this);
if(0 == Rect::w || 0 == Rect::h) return;
if(! Surface::isValid()) Set(Rect::w, Rect::h, false);
Blit(Display::Get(), *this, 0, 0);
SetDisplayFormat();
}
void Background::Save(s16 ax, s16 ay)
{
x = ax;
y = ay;
Save();
}
void Background::Save(s16 ax, s16 ay, u16 aw, u16 ah)
{
x = ax;
y = ay;
Size::w = aw;
Size::h = ah;
Save();
}
void Background::Save(const Point &pt)
{
x = pt.x;
y = pt.y;
Save();
}
void Background::Save(const Rect &rt)
{
Save(rt.x, rt.y, rt.w, rt.h);
}
void Background::Restore(void)
{
if(Surface::isValid())
Display::Get().Blit(*this, x, y);
}
const Rect & Background::GetRect(void) const
{
return *this;
}
const Point & Background::GetPos(void) const
{
return *this;
}
const Size & Background::GetSize(void) const
{
return *this;
}

View File

@@ -1,50 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2BACKGROUND_H
#define H2BACKGROUND_H
#include "types.h"
#include "rect.h"
#include "surface.h"
class Background : protected Surface, protected Rect
{
public:
Background(const Rect &rt = Rect());
Background(s16 x, s16 y, u16 w, u16 h);
Background(const Point &pt, u16 w, u16 h);
bool isValid(void) const;
void Save(void);
void Save(s16 ax, s16 ay);
void Save(s16 ax, s16 ay, u16 aw ,u16 ah);
void Save(const Point &pt);
void Save(const Rect &rt);
void Restore(void);
const Rect & GetRect(void) const;
const Point & GetPos(void) const;
const Size & GetSize(void) const;
};
#endif

View File

@@ -1,389 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include <algorithm>
#include <string>
#include "rect.h"
#include "types.h"
#include "error.h"
#include "display.h"
UpdateRects::UpdateRects() : bits(NULL), len(0), bf(0), bw(0)
{
}
UpdateRects::~UpdateRects()
{
delete [] bits;
}
void UpdateRects::SetVideoMode(u16 dw, u16 dh)
{
if(dw < 640)
{
bw = 4;
bf = 2;
}
else
if(dw > 640)
{
bw = 16;
bf = 4;
}
else
{
bw = 8;
bf = 3;
}
// fix bw and bf
while(((dw % bw) || (dh % bw)) && 1 < bf)
{
bw >>= 1;
--bf;
}
len = (dw >> bf) * (dh >> bf);
len = (len % 8 ? (len >> 3) + 1 : len >> 3);
if(bits) delete [] bits;
bits = new u8 [len];
std::fill(bits, bits + len, 0);
rects.reserve(len / 4);
}
size_t UpdateRects::Size(void) const
{
return rects.size();
}
void UpdateRects::Clear(void)
{
std::fill(bits, bits + len, 0);
rects.clear();
}
SDL_Rect* UpdateRects::Data(void)
{
return rects.size() ? &rects[0] : NULL;
}
void UpdateRects::PushRect(s16 px, s16 py, u16 pw, u16 ph)
{
Display & display = Display::Get();
if(0 != pw && 0 != ph &&
px + pw > 0 && py + ph > 0 &&
px < display.w() && py < display.h())
{
if(px < 0)
{
pw += px;
px = 0;
}
if(py < 0)
{
ph += py;
py = 0;
}
if(px + pw > display.w())
pw = display.w() - px;
if(py + ph > display.h())
ph = display.h() - py;
const u16 dw = display.w() >> bf;
s16 xx, yy;
for(yy = py; yy < py + ph; yy += bw)
for(xx = px; xx < px + pw; xx += bw)
SetBit((yy >> bf) * dw + (xx >> bf), 1);
yy = py + ph - 1;
for(xx = px; xx < px + pw; xx += bw)
SetBit((yy >> bf) * dw + (xx >> bf), 1);
xx = px + pw - 1;
for(yy = py; yy < py + ph; yy += bw)
SetBit((yy >> bf) * dw + (xx >> bf), 1);
yy = py + ph - 1;
xx = px + pw - 1;
SetBit((yy >> bf) * dw + (xx >> bf), 1);
}
}
bool UpdateRects::BitsToRects(void)
{
Display & display = Display::Get();
const u16 dbf = display.w() >> bf;
const size_t len2 = len << 3;
size_t index;
SDL_Rect rect;
SDL_Rect* prt = NULL;
for(index = 0; index < len2; ++index)
{
if(GetBit(index))
{
if(NULL != prt)
{
if(static_cast<size_t>(rect.y) == (index / dbf) * bw)
rect.w += bw;
else
{
rects.push_back(*prt);
prt = NULL;
}
}
if(NULL == prt)
{
rect.x = (index % dbf) * bw;
rect.y = (index / dbf) * bw;
rect.w = bw;
rect.h = bw;
prt = &rect;
}
}
else
{
if(prt)
{
rects.push_back(*prt);
prt = NULL;
}
}
}
if(prt)
{
rects.push_back(*prt);
prt = NULL;
}
return rects.size();
}
void UpdateRects::SetBit(u32 index, bool value)
{
if(value != GetBit(index))
bits[index >> 3] ^= (1 << (index % 8));
}
bool UpdateRects::GetBit(u32 index) const
{
return (bits[index >> 3] >> (index % 8));
}
Display::Display()
{
}
Display::~Display()
{
}
Display & Display::operator= (const Display & dp)
{
surface = SDL_GetVideoSurface();
return *this;
}
void Display::SetVideoMode(const u16 w, const u16 h, u32 flags)
{
Display & display = Display::Get();
if(display.isValid() && display.w() == w && display.h() == h) return;
if(display.surface && (display.surface->flags & SDL_FULLSCREEN)) flags |= SDL_FULLSCREEN;
display.surface = SDL_SetVideoMode(w, h, 16, flags);
if(!display.surface)
Error::Except("SDL_SetVideoMode: ", SDL_GetError());
display.update_rects.SetVideoMode(display.w(), display.h());
}
/* flip */
void Display::Flip()
{
Display & display = Display::Get();
if(display.surface->flags & SDL_HWSURFACE)
SDL_Flip(display.surface);
else
if(display.update_rects.BitsToRects())
{
SDL_UpdateRects(display.surface, display.update_rects.Size(), display.update_rects.Data());
display.update_rects.Clear();
}
}
/* full screen */
void Display::FullScreen(void)
{
Display & display = Display::Get();
SDL_WM_ToggleFullScreen(display.surface);
}
/* set caption main window */
void Display::SetCaption(const std::string & caption)
{
SDL_WM_SetCaption(caption.c_str(), NULL);
}
/* set icons window */
void Display::SetIcons(const Surface & icons)
{
SDL_WM_SetIcon(const_cast<SDL_Surface *>(icons.GetSurface()), NULL);
}
/* hide system cursor */
void Display::HideCursor(void)
{
SDL_ShowCursor(SDL_DISABLE);
}
/* show system cursor */
void Display::ShowCursor(void)
{
SDL_ShowCursor(SDL_ENABLE);
}
bool Display::Fade(u8 fadeTo)
{
Display & display = Display::Get();
u8 alpha = display.GetAlpha();
if(alpha == fadeTo) return false;
else
if(alpha < fadeTo) return Rise(fadeTo);
if(display.w() != 640 || display.h() != 480) return false;
Surface temp(display);
temp.SetDisplayFormat();
temp.Blit(display);
const u32 black = temp.MapRGB(0, 0, 0);
while(alpha > fadeTo)
{
alpha -= alpha - 10 > fadeTo ? 10 : alpha - fadeTo;
display.Fill(black);
temp.SetAlpha(alpha);
display.Blit(temp);
display.Flip();
DELAY(10);
}
return true;
}
bool Display::Rise(u8 riseTo)
{
Display & display = Display::Get();
u8 alpha = display.GetAlpha();
if(alpha == riseTo) return false;
else
if(riseTo < alpha) return Fade(riseTo);
if(display.w() != 640 || display.h() != 480) return false;
Surface temp(display);
temp.SetDisplayFormat();
temp.Blit(display);
const u32 black = temp.MapRGB(0, 0, 0);
while(alpha < riseTo)
{
alpha += alpha + 10 < riseTo ? 10 : riseTo - alpha;
display.Fill(black);
temp.SetAlpha(alpha);
display.Blit(temp);
display.Flip();
DELAY(10);
}
return true;
}
/* get video display */
Display & Display::Get(void)
{
static Display inside;
return inside;
}
int Display::GetMaxMode(Size & result, bool rotate)
{
SDL_Rect** modes = SDL_ListModes(NULL, SDL_ANYFORMAT);
if(modes == (SDL_Rect **) 0)
{
std::cerr << "Display::GetMaxMode: " << "no modes available" << std::endl;
return 0;
}
else
if(modes == (SDL_Rect **) -1)
{
//std::cout << "Display::GetMaxMode: " << "all modes available" << std::endl;
return -1;
}
else
{
int max = 0;
int cur = 0;
for(int ii = 0; modes[ii]; ++ii)
{
if(max < modes[ii]->w * modes[ii]->h)
{
max = modes[ii]->w * modes[ii]->h;
cur = ii;
}
}
result.w = modes[cur]->w;
result.h = modes[cur]->h;
if(rotate && result.w < result.h)
{
cur = result.w;
result.w = result.h;
result.h = cur;
}
}
return 1;
}
void Display::AddUpdateRect(s16 px, s16 py, u16 pw, u16 ph)
{
if(0 == (surface->flags & SDL_HWSURFACE))
update_rects.PushRect(px, py, pw, ph);
}

View File

@@ -1,84 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2DISPLAY_H
#define H2DISPLAY_H
#include <string>
#include "surface.h"
#include "rect.h"
class UpdateRects
{
public:
UpdateRects();
~UpdateRects();
void SetVideoMode(u16, u16);
void PushRect(s16, s16, u16, u16);
void Clear(void);
size_t Size(void) const;
SDL_Rect* Data(void);
bool BitsToRects(void);
protected:
void SetBit(u32, bool);
bool GetBit(u32) const;
std::vector<SDL_Rect> rects;
u8* bits;
u32 len;
u8 bf;
u8 bw;
};
class Display : public Surface
{
public:
~Display();
static Display & Get(void);
static void SetVideoMode(const u16 w, const u16 h, u32 flags);
static int GetMaxMode(Size &, bool enable_rotate);
static void HideCursor(void);
static void ShowCursor(void);
static void SetCaption(const std::string & caption);
static void SetIcons(const Surface & icons);
void AddUpdateRect(s16, s16, u16, u16);
static void Flip();
static void FullScreen(void);
static bool Fade(u8 fadeTo=0);
static bool Rise(u8 riseTo=255);
Display & operator= (const Display & dp);
private:
Display();
UpdateRects update_rects;
};
#endif

View File

@@ -1,165 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "error.h"
#include "engine.h"
#include "font.h"
#include "sdlnet.h"
namespace Mixer
{
void Init(void);
void Quit(void);
}
#ifdef WITH_AUDIOCD
namespace Cdrom
{
void Open(void);
void Close(void);
}
#endif
#ifdef _WIN32_WCE
namespace WINCE
{
bool isRunning(void);
int CreateTrayIcon(void);
void DeleteTrayIcon(void);
}
#endif
bool SDL::Init(const u32 system)
{
#ifdef _WIN32_WCE
SDL_putenv("DEBUG_VIDEO=1");
SDL_putenv("DEBUG_VIDEO_GAPI=1");
if(WINCE::isRunning()) return false;
#endif
if(0 > SDL_Init(system))
{
std::cerr << "SDL::Init: error: " << SDL_GetError() << std::endl;
return false;
}
if(SDL_INIT_AUDIO & system) Mixer::Init();
#ifdef WITH_AUDIOCD
if(SDL_INIT_CDROM & system) Cdrom::Open();
#endif
#ifdef WITH_TTF
SDL::Font::Init();
#endif
#ifdef WITH_NET
Network::Init();
#endif
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
#ifdef _WIN32_WCE
WINCE::CreateTrayIcon();
#endif
return true;
}
void SDL::Quit(void)
{
#ifdef _WIN32_WCE
WINCE::DeleteTrayIcon();
#endif
#ifdef WITH_NET
Network::Quit();
#endif
#ifdef WITH_TTF
SDL::Font::Quit();
#endif
#ifdef WITH_AUDIOCD
if(SubSystem(SDL_INIT_CDROM)) Cdrom::Close();
#endif
if(SubSystem(SDL_INIT_AUDIO)) Mixer::Quit();
SDL_Quit();
}
bool SDL::SubSystem(const u32 system)
{
return system & SDL_WasInit(system);
}
#ifdef _WIN32_WCE
#include <windows.h>
#include <shellapi.h>
#ifdef __MINGW32CE__
#undef Shell_NotifyIcon
extern "C" {
BOOL WINAPI Shell_NotifyIcon(DWORD, PNOTIFYICONDATAW);
};
#endif
// wincommon/SDL_sysevents.c
extern HICON screen_icn;
extern HINSTANCE SDL_Instance;
extern HWND SDL_Window;
bool WINCE::isRunning(void)
{
HWND hwnd = FindWindow(NULL, L"SDL_app");
if(hwnd)
{
ShowWindow(hwnd, SW_SHOW);
SetForegroundWindow(hwnd);
}
return hwnd;
}
int WINCE::CreateTrayIcon(void)
{
#ifdef ID_ICON
NOTIFYICONDATA nid = {0};
nid.cbSize = sizeof(nid);
nid.uID = ID_ICON;
nid.uFlags = NIF_ICON | NIF_MESSAGE;
nid.hWnd = SDL_Window;
nid.uCallbackMessage = WM_USER;
nid.hIcon = ::LoadIcon(SDL_Instance, MAKEINTRESOURCE(ID_ICON));
return Shell_NotifyIcon(NIM_ADD, &nid);
#endif
return 0;
}
void WINCE::DeleteTrayIcon(void)
{
#ifdef ID_ICON
NOTIFYICONDATA nid = {0};
nid.cbSize = sizeof(nid);
nid.uID = ID_ICON;
nid.hWnd = SDL_Window;
Shell_NotifyIcon(NIM_DELETE, &nid);
#endif
}
#endif

View File

@@ -1,54 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2ENGINE_H
#define H2ENGINE_H
#include "background.h"
#include "display.h"
#include "localevent.h"
#include "error.h"
#include "rect.h"
#include "spritecursor.h"
#include "surface.h"
#include "palette.h"
#include "rand.h"
#include "tools.h"
#include "audio.h"
#include "audio_mixer.h"
#include "audio_music.h"
#include "audio_cdrom.h"
#include "types.h"
#define INIT_VIDEO SDL_INIT_VIDEO
#define INIT_AUDIO SDL_INIT_AUDIO
#define INIT_TIMER SDL_INIT_TIMER
#define INIT_CDROM SDL_INIT_CDROM
namespace SDL
{
bool Init(const u32 system = INIT_VIDEO);
void Quit(void);
bool SubSystem(const u32 system);
}
#endif

View File

@@ -1,32 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdexcept>
#include <iostream>
#include "error.h"
/* exception */
void Error::Except(const char* message, const char* cstr)
{
std::cerr << "Error::Except: " << message << cstr << std::endl;
throw Exception();
}

View File

@@ -1,38 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2ERROR_H
#define H2ERROR_H
#include <string>
class Error
{
public:
Error(){};
~Error(){};
class Exception{};
static void Except(const char*, const char*);
};
#endif

View File

@@ -1,143 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef WITH_TTF
#include <iostream>
#include "font.h"
#include "engine.h"
#include "surface.h"
#include "SDL_ttf.h"
bool SDL::Font::init = false;
SDL::Font::Font() : fnt(NULL)
{
}
SDL::Font::~Font()
{
if(fnt) TTF_CloseFont(fnt);
}
void SDL::Font::Init(void)
{
if(0 != TTF_Init()) std::cerr << "Font::Init: error" << std::endl;
else init = true;
}
void SDL::Font::Quit(void)
{
TTF_Quit();
init = false;
}
bool SDL::Font::isValid(void) const
{
return fnt;
}
bool SDL::Font::Open(const std::string & filename, u8 size)
{
if(init)
{
if(fnt) TTF_CloseFont(fnt);
fnt = TTF_OpenFont(filename.c_str(), size);
if(!fnt) std::cerr << "Font::Open: error open: " << filename << std::endl;
}
return fnt;
}
void SDL::Font::SetStyle(u8 style)
{
if(fnt) TTF_SetFontStyle(fnt, style);
}
void SDL::Font::RenderText(Surface & dst, const std::string & msg, const Colors & clr, render_t render)
{
if(dst.surface) Surface::FreeSurface(dst);
if(fnt) switch(render)
{
case BLENDED: dst.surface = TTF_RenderUTF8_Blended(fnt, msg.c_str(), clr); break;
default: dst.surface = TTF_RenderUTF8_Solid(fnt, msg.c_str(), clr); break;
}
}
void SDL::Font::RenderChar(Surface & dst, char ch, const Colors & clr, render_t render)
{
char buf[2] = { '\0', '\0' };
buf[0] = ch;
if(dst.surface) Surface::FreeSurface(dst);
if(fnt) switch(render)
{
case BLENDED: dst.surface = TTF_RenderUTF8_Blended(fnt, buf, clr); break;
default: dst.surface = TTF_RenderUTF8_Solid(fnt, buf, clr); break;
}
}
void SDL::Font::RenderUnicodeText(Surface & dst, const u16 *msg, const Colors & clr, render_t render)
{
if(dst.surface) Surface::FreeSurface(dst);
if(fnt) switch(render)
{
case BLENDED: dst.surface = TTF_RenderUNICODE_Blended(fnt, msg, clr); break;
default: dst.surface = TTF_RenderUNICODE_Solid(fnt, msg, clr); break;
}
}
void SDL::Font::RenderUnicodeChar(Surface & dst, u16 ch, const Colors & clr, render_t render)
{
u16 buf[2] = { L'\0', L'\0' };
buf[0] = ch;
if(dst.surface) Surface::FreeSurface(dst);
if(fnt) switch(render)
{
case BLENDED: dst.surface = TTF_RenderUNICODE_Blended(fnt, buf, clr); break;
default: dst.surface = TTF_RenderUNICODE_Solid(fnt, buf, clr); break;
}
}
int SDL::Font::Height(void) const
{
return fnt ? TTF_FontHeight(fnt) : 0;
}
int SDL::Font::Ascent(void) const
{
return fnt ? TTF_FontAscent(fnt) : 0;
}
int SDL::Font::Descent(void) const
{
return fnt ? TTF_FontDescent(fnt) : 0;
}
int SDL::Font::LineSkip(void) const
{
return fnt ? TTF_FontLineSkip(fnt) : 0;
}
#endif

View File

@@ -1,68 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2FONT_H
#define H2FONT_H
#ifdef WITH_TTF
#include <string>
#include "types.h"
#include "SDL_ttf.h"
class Surface;
namespace SDL
{
class Font
{
public:
enum render_t { SOLID, BLENDED };
Font();
~Font();
static void Init(void);
static void Quit(void);
bool Open(const std::string &, u8);
bool isValid(void) const;
void SetStyle(u8);
int Height(void) const;
int Ascent(void) const;
int Descent(void) const;
int LineSkip(void) const;
void RenderText(Surface &, const std::string &, const Colors &, render_t = SOLID);
void RenderChar(Surface &, char, const Colors &, render_t = SOLID);
void RenderUnicodeText(Surface &, const u16 *, const Colors &, render_t = SOLID);
void RenderUnicodeChar(Surface &, u16, const Colors &, render_t = SOLID);
private:
TTF_Font *fnt;
static bool init;
};
}
#endif
#endif

View File

@@ -1,883 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "error.h"
#include "display.h"
#include "audio_music.h"
#include "audio_mixer.h"
#include "localevent.h"
#define TAP_DELAY_EMULATE 1050
LocalEvent::LocalEvent() : modes(0), key_value(KEY_NONE), mouse_state(0),
mouse_button(0), mouse_st(0, 0), redraw_cursor_func(NULL), keyboard_filter_func(NULL),
clock_delay(TAP_DELAY_EMULATE), loop_delay(1)
{
#ifdef WITHOUT_MOUSE
emulate_mouse = false;
emulate_mouse_up = KEY_UP;
emulate_mouse_down = KEY_DOWN;
emulate_mouse_left = KEY_LEFT;
emulate_mouse_right = KEY_RIGHT;
emulate_mouse_step = 10;
emulate_press_left = KEY_NONE;
emulate_press_right = KEY_NONE;
#endif
}
const Point & LocalEvent::GetMousePressLeft(void) const
{
return mouse_pl;
}
const Point & LocalEvent::GetMousePressMiddle(void) const
{
return mouse_pm;
}
const Point & LocalEvent::GetMousePressRight(void) const
{
return mouse_pr;
}
const Point & LocalEvent::GetMouseReleaseLeft(void) const
{
return mouse_rl;
}
const Point & LocalEvent::GetMouseReleaseMiddle(void) const
{
return mouse_rm;
}
const Point & LocalEvent::GetMouseReleaseRight(void) const
{
return mouse_rr;
}
void LocalEvent::SetTapMode(bool f)
{
if(f)
SetModes(TAP_MODE);
else
{
ResetModes(TAP_MODE);
ResetModes(CLOCK_ON);
clock.Stop();
}
}
void LocalEvent::SetTapDelayForRightClickEmulation(u32 d)
{
clock_delay = d < 200 ? TAP_DELAY_EMULATE : d;
}
void LocalEvent::SetMouseOffsetX(s16 x)
{
SetModes(MOUSE_OFFSET);
mouse_st.x = x;
}
void LocalEvent::SetMouseOffsetY(s16 y)
{
SetModes(MOUSE_OFFSET);
mouse_st.y = y;
}
void LocalEvent::SetModes(flag_t f)
{
modes |= f;
}
void LocalEvent::ResetModes(flag_t f)
{
modes &= ~f;
}
void LocalEvent::SetGlobalFilter(bool f)
{
f ? SetModes(GLOBAL_FILTER) : ResetModes(GLOBAL_FILTER);
}
const char* KeySymGetName(KeySym sym)
{
return SDL_GetKeyName(static_cast<SDLKey>(sym));
}
KeySym GetKeySym(int key)
{
switch(key)
{
default: break;
case SDLK_RETURN: return KEY_RETURN;
case SDLK_LEFT: return KEY_LEFT;
case SDLK_RIGHT: return KEY_RIGHT;
case SDLK_UP: return KEY_UP;
case SDLK_DOWN: return KEY_DOWN;
case SDLK_ESCAPE: return KEY_ESCAPE;
case SDLK_KP_ENTER: return KEY_RETURN;
case SDLK_BACKSPACE: return KEY_BACKSPACE;
case SDLK_EXCLAIM: return KEY_EXCLAIM;
case SDLK_QUOTEDBL: return KEY_QUOTEDBL;
case SDLK_HASH: return KEY_HASH;
case SDLK_DOLLAR: return KEY_DOLLAR;
case SDLK_AMPERSAND: return KEY_AMPERSAND;
case SDLK_QUOTE: return KEY_QUOTE;
case SDLK_LEFTPAREN: return KEY_LEFTPAREN;
case SDLK_RIGHTPAREN: return KEY_RIGHTPAREN;
case SDLK_ASTERISK: return KEY_ASTERISK;
case SDLK_PLUS: return KEY_PLUS;
case SDLK_COMMA: return KEY_COMMA;
case SDLK_MINUS: return KEY_MINUS;
case SDLK_PERIOD: return KEY_PERIOD;
case SDLK_SLASH: return KEY_SLASH;
case SDLK_COLON: return KEY_COLON;
case SDLK_SEMICOLON: return KEY_SEMICOLON;
case SDLK_LESS: return KEY_LESS;
case SDLK_EQUALS: return KEY_EQUALS;
case SDLK_GREATER: return KEY_GREATER;
case SDLK_QUESTION: return KEY_QUESTION;
case SDLK_AT: return KEY_AT;
case SDLK_LEFTBRACKET: return KEY_LEFTBRACKET;
case SDLK_BACKSLASH: return KEY_BACKSLASH;
case SDLK_RIGHTBRACKET: return KEY_RIGHTBRACKET;
case SDLK_CARET: return KEY_CARET;
case SDLK_UNDERSCORE: return KEY_UNDERSCORE;
case SDLK_LALT: return KEY_ALT;
case SDLK_RALT: return KEY_ALT;
case SDLK_LCTRL: return KEY_CONTROL;
case SDLK_RCTRL: return KEY_CONTROL;
case SDLK_LSHIFT: return KEY_SHIFT;
case SDLK_RSHIFT: return KEY_SHIFT;
case SDLK_TAB: return KEY_TAB;
case SDLK_SPACE: return KEY_SPACE;
case SDLK_DELETE: return KEY_DELETE;
case SDLK_PAGEUP: return KEY_PAGEUP;
case SDLK_PAGEDOWN: return KEY_PAGEDOWN;
case SDLK_F1: return KEY_F1;
case SDLK_F2: return KEY_F2;
case SDLK_F3: return KEY_F3;
case SDLK_F4: return KEY_F4;
case SDLK_F5: return KEY_F5;
case SDLK_F6: return KEY_F6;
case SDLK_F7: return KEY_F7;
case SDLK_F8: return KEY_F8;
case SDLK_F9: return KEY_F9;
case SDLK_F10: return KEY_F10;
case SDLK_F11: return KEY_F11;
case SDLK_F12: return KEY_F12;
case SDLK_PRINT: return KEY_PRINT;
case SDLK_0: return KEY_0;
case SDLK_1: return KEY_1;
case SDLK_2: return KEY_2;
case SDLK_3: return KEY_3;
case SDLK_4: return KEY_4;
case SDLK_5: return KEY_5;
case SDLK_6: return KEY_6;
case SDLK_7: return KEY_7;
case SDLK_8: return KEY_8;
case SDLK_9: return KEY_9;
case SDLK_a: return KEY_a;
case SDLK_b: return KEY_b;
case SDLK_c: return KEY_c;
case SDLK_d: return KEY_d;
case SDLK_e: return KEY_e;
case SDLK_f: return KEY_f;
case SDLK_g: return KEY_g;
case SDLK_h: return KEY_h;
case SDLK_i: return KEY_i;
case SDLK_j: return KEY_j;
case SDLK_k: return KEY_k;
case SDLK_l: return KEY_l;
case SDLK_m: return KEY_m;
case SDLK_n: return KEY_n;
case SDLK_o: return KEY_o;
case SDLK_p: return KEY_p;
case SDLK_q: return KEY_q;
case SDLK_r: return KEY_r;
case SDLK_s: return KEY_s;
case SDLK_t: return KEY_t;
case SDLK_u: return KEY_u;
case SDLK_v: return KEY_v;
case SDLK_w: return KEY_w;
case SDLK_x: return KEY_x;
case SDLK_y: return KEY_y;
case SDLK_z: return KEY_z;
#ifdef _WIN32_WCE
case 0xC1: return KEY_APP01;
case 0xC2: return KEY_APP02;
case 0xC3: return KEY_APP03;
case 0xC4: return KEY_APP04;
case 0xC5: return KEY_APP05;
case 0xC6: return KEY_APP06;
case 0xC7: return KEY_APP07;
case 0xC8: return KEY_APP08;
case 0xC9: return KEY_APP09;
case 0xCA: return KEY_APP10;
case 0xCB: return KEY_APP11;
case 0xCC: return KEY_APP12;
case 0xCD: return KEY_APP13;
case 0xCE: return KEY_APP14;
case 0xCF: return KEY_APP15;
#endif
}
return KEY_NONE;
}
LocalEvent & LocalEvent::Get(void)
{
static LocalEvent le;
return le;
}
bool LocalEvent::HandleEvents(bool delay)
{
SDL_Event event;
ResetModes(MOUSE_MOTION);
ResetModes(KEY_PRESSED);
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_ACTIVEEVENT:
if(event.active.state & SDL_APPACTIVE)
{
if(Mixer::isValid())
{
//iconify
if(0 == event.active.gain)
{
Mixer::Reset();
Music::Pause();
loop_delay = 100;
}
else
loop_delay = 1;
}
}
break;
// keyboard
case SDL_KEYDOWN:
case SDL_KEYUP:
HandleKeyboardEvent(event.key);
break;
// mouse motion
case SDL_MOUSEMOTION:
HandleMouseMotionEvent(event.motion);
break;
// mouse button
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
HandleMouseButtonEvent(event.button);
break;
// exit
case SDL_QUIT:
Error::Except("LocalEvent::HandleEvents: ", "quit event: ok.");
return false;
default:
break;
}
// need for wheel up/down delay
if(SDL_BUTTON_WHEELDOWN == event.button.button || SDL_BUTTON_WHEELUP == event.button.button) break;
}
// emulate press right
if((modes & TAP_MODE) && (modes & CLOCK_ON))
{
clock.Stop();
if(clock_delay < clock.Get())
{
ResetModes(CLICK_LEFT);
ResetModes(CLOCK_ON);
mouse_pr = mouse_cu;
SetModes(MOUSE_PRESSED);
mouse_button = SDL_BUTTON_RIGHT;
}
}
if(delay) SDL_Delay(loop_delay);
return true;
}
bool LocalEvent::MouseMotion(void) const
{
return modes & MOUSE_MOTION;
}
bool LocalEvent::MouseMotion(const Rect &rt) const
{
return modes & MOUSE_MOTION ? rt & mouse_cu : false;
}
bool LocalEvent::MousePressLeft(void) const
{
return (modes & MOUSE_PRESSED) && SDL_BUTTON_LEFT == mouse_button;
}
bool LocalEvent::MouseReleaseLeft(void) const
{
return !(modes & MOUSE_PRESSED) && SDL_BUTTON_LEFT == mouse_button;
}
bool LocalEvent::MousePressMiddle(void) const
{
return (modes & MOUSE_PRESSED) && SDL_BUTTON_MIDDLE == mouse_button;
}
bool LocalEvent::MouseReleaseMiddle(void) const
{
return !(modes & MOUSE_PRESSED) && SDL_BUTTON_MIDDLE == mouse_button;
}
bool LocalEvent::MousePressRight(void) const
{
return (modes & MOUSE_PRESSED) && SDL_BUTTON_RIGHT == mouse_button;
}
bool LocalEvent::MouseReleaseRight(void) const
{
return !(modes & MOUSE_PRESSED) && SDL_BUTTON_RIGHT == mouse_button;
}
void LocalEvent::HandleKeyboardEvent(SDL_KeyboardEvent & event)
{
if(KEY_NONE != GetKeySym(event.keysym.sym))
{
(event.type == SDL_KEYDOWN) ? SetModes(KEY_PRESSED) : ResetModes(KEY_PRESSED);
#ifdef WITHOUT_MOUSE
if(emulate_mouse && EmulateMouseAction(GetKeySym(event.keysym.sym))) return;
#endif
key_value = GetKeySym(event.keysym.sym);
}
}
void LocalEvent::HandleMouseMotionEvent(const SDL_MouseMotionEvent & motion)
{
mouse_state = motion.state;
SetModes(MOUSE_MOTION);
mouse_cu.x = motion.x;
mouse_cu.y = motion.y;
if(modes & MOUSE_OFFSET) mouse_cu += mouse_st;
}
void LocalEvent::HandleMouseButtonEvent(const SDL_MouseButtonEvent & button)
{
button.state == SDL_PRESSED ? SetModes(MOUSE_PRESSED) : ResetModes(MOUSE_PRESSED);
mouse_button = button.button;
mouse_cu.x = button.x;
mouse_cu.y = button.y;
if(modes & MOUSE_OFFSET) mouse_cu += mouse_st;
if(modes & MOUSE_PRESSED)
switch(button.button)
{
case SDL_BUTTON_WHEELDOWN:
case SDL_BUTTON_WHEELUP:
mouse_pm = mouse_cu;
break;
case SDL_BUTTON_LEFT:
mouse_pl = mouse_cu;
SetModes(CLICK_LEFT);
// emulate press right
if(modes & TAP_MODE){ clock.Start(); SetModes(CLOCK_ON); }
break;
case SDL_BUTTON_MIDDLE:
mouse_pm = mouse_cu;
SetModes(CLICK_MIDDLE);
break;
case SDL_BUTTON_RIGHT:
mouse_pr = mouse_cu;
SetModes(CLICK_RIGHT);
break;
default:
break;
}
else
switch(button.button)
{
case SDL_BUTTON_WHEELDOWN:
case SDL_BUTTON_WHEELUP:
mouse_rm = mouse_cu;
break;
case SDL_BUTTON_LEFT:
mouse_rl = mouse_cu;
// emulate press right
if(modes & TAP_MODE){ ResetModes(CLOCK_ON); }
break;
case SDL_BUTTON_MIDDLE:
mouse_rm = mouse_cu;
break;
case SDL_BUTTON_RIGHT:
mouse_rr = mouse_cu;
break;
default:
break;
}
}
bool LocalEvent::MouseClickLeft(void)
{
if(MouseReleaseLeft() && (CLICK_LEFT & modes))
{
ResetModes(CLICK_LEFT);
return true;
}
return false;
}
bool LocalEvent::MouseClickLeft(const Rect &rt)
{
//if(MouseReleaseLeft() && (rt & mouse_rl) && (CLICK_LEFT & modes) && ((modes & TAP_MODE) || (rt & mouse_pl)))
if(MouseReleaseLeft() && (rt & mouse_pl) && (rt & mouse_rl) && (CLICK_LEFT & modes))
{
ResetModes(CLICK_LEFT);
return true;
}
return false;
}
bool LocalEvent::MouseClickMiddle(void)
{
if(MouseReleaseMiddle() && (CLICK_MIDDLE & modes))
{
ResetModes(CLICK_MIDDLE);
return true;
}
return false;
}
bool LocalEvent::MouseClickMiddle(const Rect &rt)
{
if(MouseReleaseMiddle() && (rt & mouse_pm) && (rt & mouse_rm) && (CLICK_MIDDLE & modes))
{
ResetModes(CLICK_MIDDLE);
return true;
}
return false;
}
bool LocalEvent::MouseClickRight(void)
{
if(MouseReleaseRight() && (CLICK_RIGHT & modes))
{
ResetModes(CLICK_RIGHT);
return true;
}
return false;
}
bool LocalEvent::MouseClickRight(const Rect &rt)
{
if(MouseReleaseRight() && (rt & mouse_pr) && (rt & mouse_rr) && (CLICK_RIGHT & modes))
{
ResetModes(CLICK_RIGHT);
return true;
}
return false;
}
bool LocalEvent::MouseWheelUp(void) const
{
return (modes & MOUSE_PRESSED) && SDL_BUTTON_WHEELUP == mouse_button;
}
bool LocalEvent::MouseWheelDn(void) const
{
return (modes & MOUSE_PRESSED) && SDL_BUTTON_WHEELDOWN == mouse_button;
}
bool LocalEvent::MousePressLeft(const Rect &rt) const
{
return MousePressLeft() && (rt & mouse_pl);
}
bool LocalEvent::MousePressLeft(const Point &pt, u16 w, u16 h) const
{
return MousePressLeft() && (Rect(pt.x, pt.y, w, h) & mouse_pl);
}
bool LocalEvent::MousePressMiddle(const Rect &rt) const
{
return MousePressMiddle() && (rt & mouse_pm);
}
bool LocalEvent::MousePressRight(const Rect &rt) const
{
return MousePressRight() && (rt & mouse_pr);
}
bool LocalEvent::MouseReleaseLeft(const Rect &rt) const
{
return MouseReleaseLeft() && (rt & mouse_rl);
}
bool LocalEvent::MouseReleaseMiddle(const Rect &rt) const
{
return MouseReleaseMiddle() && (rt & mouse_rm);
}
bool LocalEvent::MouseReleaseRight(const Rect &rt) const
{
return MouseReleaseRight() && (rt & mouse_rr);
}
void LocalEvent::ResetPressLeft(void)
{
mouse_pl.x = -1;
mouse_pl.y = -1;
}
void LocalEvent::ResetPressRight(void)
{
mouse_pr.x = -1;
mouse_pr.y = -1;
}
void LocalEvent::ResetPressMiddle(void)
{
mouse_pm.x = -1;
mouse_pm.y = -1;
}
void LocalEvent::ResetReleaseLeft(void)
{
mouse_rl.x = -1;
mouse_rl.y = -1;
}
void LocalEvent::ResetReleaseRight(void)
{
mouse_rr.x = -1;
mouse_rr.y = -1;
}
void LocalEvent::ResetReleaseMiddle(void)
{
mouse_rm.x = -1;
mouse_rm.y = -1;
}
bool LocalEvent::MouseWheelUp(const Rect &rt) const
{
return MouseWheelUp() && (rt & mouse_cu);
}
bool LocalEvent::MouseWheelDn(const Rect &rt) const
{
return MouseWheelDn() && (rt & mouse_cu);
}
bool LocalEvent::MouseCursor(const Rect &rt) const
{
return rt & mouse_cu;
}
const Point & LocalEvent::GetMouseCursor(void)
{
#ifdef WITHOUT_MOUSE
if(!emulate_mouse)
#endif
{
int x, y;
SDL_PumpEvents();
SDL_GetMouseState(&x, &y);
mouse_cu.x = x;
mouse_cu.y = y;
}
if(modes & MOUSE_OFFSET) mouse_cu += mouse_st;
return mouse_cu;
}
u16 LocalEvent::KeyMod(void) const
{
return SDL_GetModState();
}
KeySym LocalEvent::KeyValue(void) const
{
return key_value;
}
bool LocalEvent::KeyPress(void) const
{
return modes & KEY_PRESSED;
}
bool LocalEvent::KeyPress(KeySym key) const
{
return key == key_value && (modes & KEY_PRESSED);
}
void LocalEvent::SetGlobalFilterMouseEvents(void (*pf)(u16, u16))
{
redraw_cursor_func = pf;
}
void LocalEvent::SetGlobalFilterKeysEvents(void (*pf)(int, u16))
{
keyboard_filter_func = pf;
}
#if SDL_VERSION_ATLEAST(1, 3, 0)
int LocalEvent::GlobalFilterEvents(void *userdata, SDL_Event *event)
#else
int LocalEvent::GlobalFilterEvents(const SDL_Event *event)
#endif
{
LocalEvent & le = LocalEvent::Get();
// motion
if((le.modes & GLOBAL_FILTER) && SDL_MOUSEMOTION == event->type)
{
// redraw cursor
if(le.redraw_cursor_func)
{
if(le.modes & MOUSE_OFFSET)
(*(le.redraw_cursor_func))(event->motion.x + le.mouse_st.x, event->motion.y + le.mouse_st.y);
else
(*(le.redraw_cursor_func))(event->motion.x, event->motion.y);
}
}
// key
if((le.modes & GLOBAL_FILTER) && SDL_KEYDOWN == event->type)
{
// key event
if(le.keyboard_filter_func)
(*(le.keyboard_filter_func))(event->key.keysym.sym, event->key.keysym.mod);
}
return 1;
}
void LocalEvent::SetState(u32 type, bool enable)
{
SDL_EventState(type, enable ? SDL_ENABLE : SDL_IGNORE);
}
u8 LocalEvent::GetState(u32 type)
{
return SDL_EventState(type, SDL_QUERY);
}
void LocalEvent::SetStateDefaults(void)
{
// enable events
SetState(SDL_ACTIVEEVENT, true);
SetState(SDL_USEREVENT, true);
SetState(SDL_KEYDOWN, true);
SetState(SDL_KEYUP, true);
SetState(SDL_MOUSEMOTION, true);
SetState(SDL_MOUSEBUTTONDOWN, true);
SetState(SDL_MOUSEBUTTONUP, true);
SetState(SDL_QUIT, true);
// ignore events
SetState(SDL_JOYAXISMOTION, false);
SetState(SDL_JOYBALLMOTION, false);
SetState(SDL_JOYHATMOTION, false);
SetState(SDL_JOYBUTTONUP, false);
SetState(SDL_JOYBUTTONDOWN, false);
SetState(SDL_SYSWMEVENT, false);
SetState(SDL_VIDEORESIZE, false);
SetState(SDL_VIDEOEXPOSE, false);
#if SDL_VERSION_ATLEAST(1, 3, 0)
SDL_SetEventFilter(GlobalFilterEvents, NULL);
#else
SDL_SetEventFilter(GlobalFilterEvents);
#endif
}
#ifdef WITHOUT_MOUSE
void LocalEvent::ToggleEmulateMouse(void)
{
emulate_mouse = emulate_mouse ? false : true;
}
void LocalEvent::SetEmulateMouse(bool f)
{
emulate_mouse = f;
if(f) mouse_cu = Point(0, 0);
}
void LocalEvent::SetEmulateMouseUpKey(KeySym k)
{
emulate_mouse_up = k;
}
void LocalEvent::SetEmulateMouseDownKey(KeySym k)
{
emulate_mouse_down = k;
}
void LocalEvent::SetEmulateMouseLeftKey(KeySym k)
{
emulate_mouse_left = k;
}
void LocalEvent::SetEmulateMouseRightKey(KeySym k)
{
emulate_mouse_right = k;
}
void LocalEvent::SetEmulateMouseStep(u8 s)
{
emulate_mouse_step = s;
}
void LocalEvent::SetEmulatePressLeftKey(KeySym k)
{
emulate_press_left = k;
}
void LocalEvent::SetEmulatePressRightKey(KeySym k)
{
emulate_press_right = k;
}
bool LocalEvent::EmulateMouseAction(KeySym key)
{
if((key == emulate_mouse_up ||
key == emulate_mouse_down ||
key == emulate_mouse_left ||
key == emulate_mouse_right ||
key == emulate_press_left ||
key == emulate_press_right))
{
if(emulate_mouse_up == key)
{
mouse_cu.y -= emulate_mouse_step;
SetModes(MOUSE_MOTION);
}
else
if(emulate_mouse_down == key)
{
mouse_cu.y += emulate_mouse_step;
SetModes(MOUSE_MOTION);
}
else
if(emulate_mouse_left == key)
{
mouse_cu.x -= emulate_mouse_step;
SetModes(MOUSE_MOTION);
}
else
if(emulate_mouse_right == key)
{
mouse_cu.x += emulate_mouse_step;
SetModes(MOUSE_MOTION);
}
if(mouse_cu.x < 0) mouse_cu.x = 0;
if(mouse_cu.y < 0) mouse_cu.y = 0;
if(mouse_cu.x > Display::Get().w()) mouse_cu.x = Display::Get().w();
if(mouse_cu.y > Display::Get().h()) mouse_cu.y = Display::Get().h();
if(emulate_press_left == key)
{
if(modes & KEY_PRESSED)
{
mouse_pl = mouse_cu;
SetModes(MOUSE_PRESSED);
SetModes(CLICK_LEFT);
}
else
{
mouse_rl = mouse_cu;
ResetModes(MOUSE_PRESSED);
}
mouse_button = SDL_BUTTON_LEFT;
}
else
if(emulate_press_right == key)
{
if(modes & KEY_PRESSED)
{
mouse_pr = mouse_cu;
SetModes(MOUSE_PRESSED);
}
else
{
mouse_rr = mouse_cu;
ResetModes(MOUSE_PRESSED);
}
mouse_button = SDL_BUTTON_RIGHT;
}
if((modes & MOUSE_MOTION) && redraw_cursor_func)
{
if(modes & MOUSE_OFFSET)
(*(redraw_cursor_func))(mouse_cu.x + mouse_st.x, mouse_cu.y + mouse_st.y);
else
(*(redraw_cursor_func))(mouse_cu.x, mouse_cu.y);
}
ResetModes(KEY_PRESSED);
return true;
}
return false;
}
#endif

View File

@@ -1,306 +0,0 @@
/***************************************************************************
* Copyright (C) 2006 by Andrey Afletdinov <fheroes2@gmail.com> *
* Copyright (C) 2008 by Josh Matthews <josh@joshmatthews.net> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2LOCALEVENT_H
#define H2LOCALEVENT_H
#include "rect.h"
#include "thread.h"
#include "types.h"
enum KeyMod { MOD_NONE = KMOD_NONE, MOD_CTRL = KMOD_CTRL, MOD_SHIFT = KMOD_SHIFT, MOD_ALT = KMOD_ALT, MOD_CAPS = KMOD_CAPS };
enum KeySym
{
KEY_NONE = -1,
KEY_UNKNOWN = SDLK_UNKNOWN,
KEY_BACKSPACE = SDLK_BACKSPACE,
KEY_RETURN = SDLK_RETURN,
KEY_ESCAPE = SDLK_ESCAPE,
KEY_SPACE = SDLK_SPACE,
KEY_EXCLAIM = SDLK_EXCLAIM,
KEY_QUOTEDBL = SDLK_QUOTEDBL,
KEY_HASH = SDLK_HASH,
KEY_DOLLAR = SDLK_DOLLAR,
KEY_AMPERSAND = SDLK_AMPERSAND,
KEY_QUOTE = SDLK_QUOTE,
KEY_LEFTPAREN = SDLK_LEFTPAREN,
KEY_RIGHTPAREN = SDLK_RIGHTPAREN,
KEY_ASTERISK = SDLK_ASTERISK,
KEY_PLUS = SDLK_PLUS,
KEY_COMMA = SDLK_COMMA,
KEY_MINUS = SDLK_MINUS,
KEY_PERIOD = SDLK_PERIOD,
KEY_SLASH = SDLK_SLASH,
KEY_COLON = SDLK_COLON,
KEY_SEMICOLON = SDLK_SEMICOLON,
KEY_LESS = SDLK_LESS,
KEY_EQUALS = SDLK_EQUALS,
KEY_GREATER = SDLK_GREATER,
KEY_QUESTION = SDLK_QUESTION,
KEY_AT = SDLK_AT,
KEY_LEFTBRACKET = SDLK_LEFTBRACKET,
KEY_BACKSLASH = SDLK_BACKSLASH,
KEY_RIGHTBRACKET = SDLK_RIGHTBRACKET,
KEY_CARET = SDLK_CARET,
KEY_UNDERSCORE = SDLK_UNDERSCORE,
KEY_ALT = SDLK_LALT,
KEY_CONTROL = SDLK_LCTRL,
KEY_SHIFT = SDLK_LSHIFT,
KEY_TAB = SDLK_TAB,
KEY_DELETE = SDLK_DELETE,
KEY_PAGEUP = SDLK_PAGEUP,
KEY_PAGEDOWN = SDLK_PAGEDOWN,
KEY_F1 = SDLK_F1,
KEY_F2 = SDLK_F2,
KEY_F3 = SDLK_F3,
KEY_F4 = SDLK_F4,
KEY_F5 = SDLK_F5,
KEY_F6 = SDLK_F6,
KEY_F7 = SDLK_F7,
KEY_F8 = SDLK_F8,
KEY_F9 = SDLK_F9,
KEY_F10 = SDLK_F10,
KEY_F11 = SDLK_F11,
KEY_F12 = SDLK_F12,
KEY_PRINT = SDLK_PRINT,
KEY_LEFT = SDLK_LEFT,
KEY_RIGHT = SDLK_RIGHT,
KEY_UP = SDLK_UP,
KEY_DOWN = SDLK_DOWN,
KEY_0 = SDLK_0,
KEY_1 = SDLK_1,
KEY_2 = SDLK_2,
KEY_3 = SDLK_3,
KEY_4 = SDLK_4,
KEY_5 = SDLK_5,
KEY_6 = SDLK_6,
KEY_7 = SDLK_7,
KEY_8 = SDLK_8,
KEY_9 = SDLK_9,
KEY_a = SDLK_a,
KEY_b = SDLK_b,
KEY_c = SDLK_c,
KEY_d = SDLK_d,
KEY_e = SDLK_e,
KEY_f = SDLK_f,
KEY_g = SDLK_g,
KEY_h = SDLK_h,
KEY_i = SDLK_i,
KEY_j = SDLK_j,
KEY_k = SDLK_k,
KEY_l = SDLK_l,
KEY_m = SDLK_m,
KEY_n = SDLK_n,
KEY_o = SDLK_o,
KEY_p = SDLK_p,
KEY_q = SDLK_q,
KEY_r = SDLK_r,
KEY_s = SDLK_s,
KEY_t = SDLK_t,
KEY_u = SDLK_u,
KEY_v = SDLK_v,
KEY_w = SDLK_w,
KEY_x = SDLK_x,
KEY_y = SDLK_y,
KEY_z = SDLK_z,
#ifdef _WIN32_WCE
KEY_APP01 = 0xC1,
KEY_APP02 = 0xC2,
KEY_APP03 = 0xC3,
KEY_APP04 = 0xC4,
KEY_APP05 = 0xC5,
KEY_APP06 = 0xC6,
KEY_APP07 = 0xC7,
KEY_APP08 = 0xC8,
KEY_APP09 = 0xC9,
KEY_APP10 = 0xCA,
KEY_APP11 = 0xCB,
KEY_APP12 = 0xCC,
KEY_APP13 = 0xCD,
KEY_APP14 = 0xCE,
KEY_APP15 = 0xCF,
#endif
KEY_LAST
};
const char* KeySymGetName(KeySym);
KeySym GetKeySym(int);
class LocalEvent
{
public:
static LocalEvent & Get(void);
void SetGlobalFilterMouseEvents(void (*pf)(u16, u16));
void SetGlobalFilterKeysEvents(void (*pf)(int, u16));
void SetGlobalFilter(bool);
void SetTapMode(bool);
void SetTapDelayForRightClickEmulation(u32);
void SetMouseOffsetX(s16);
void SetMouseOffsetY(s16);
static void SetStateDefaults(void);
static void SetState(u32 type, bool enable);
static u8 GetState(u32 type);
bool HandleEvents(bool delay = true);
bool MouseMotion(void) const;
bool MouseMotion(const Rect &rt) const;
const Point & GetMouseCursor(void);
const Point & GetMousePressLeft(void) const;
const Point & GetMousePressMiddle(void) const;
const Point & GetMousePressRight(void) const;
const Point & GetMouseReleaseLeft(void) const;
const Point & GetMouseReleaseMiddle(void) const;
const Point & GetMouseReleaseRight(void) const;
void ResetPressLeft(void);
void ResetPressRight(void);
void ResetPressMiddle(void);
void ResetReleaseLeft(void);
void ResetReleaseRight(void);
void ResetReleaseMiddle(void);
bool MouseClickLeft(void);
bool MouseClickMiddle(void);
bool MouseClickRight(void);
bool MouseClickLeft(const Rect &rt);
bool MouseClickMiddle(const Rect &rt);
bool MouseClickRight(const Rect &rt);
bool MouseWheelUp(void) const;
bool MouseWheelDn(void) const;
bool MousePressLeft(void) const;
bool MousePressLeft(const Rect &rt) const;
bool MousePressLeft(const Point &pt, u16 w, u16 h) const;
bool MousePressMiddle(void) const;
bool MousePressMiddle(const Rect &rt) const;
bool MousePressRight(void) const;
bool MousePressRight(const Rect &rt) const;
bool MouseReleaseLeft(void) const;
bool MouseReleaseLeft(const Rect &rt) const;
bool MouseReleaseMiddle(void) const;
bool MouseReleaseMiddle(const Rect &rt) const;
bool MouseReleaseRight(void) const;
bool MouseReleaseRight(const Rect &rt) const;
bool MouseWheelUp(const Rect &rt) const;
bool MouseWheelDn(const Rect &rt) const;
bool MouseCursor(const Rect &rt) const;
bool KeyPress(void) const;
bool KeyPress(KeySym key) const;
KeySym KeyValue(void) const;
u16 KeyMod(void) const;
#ifdef WITHOUT_MOUSE
void ToggleEmulateMouse(void);
void SetEmulateMouse(bool);
void SetEmulateMouseUpKey(KeySym);
void SetEmulateMouseDownKey(KeySym);
void SetEmulateMouseLeftKey(KeySym);
void SetEmulateMouseRightKey(KeySym);
void SetEmulateMouseStep(u8);
void SetEmulatePressLeftKey(KeySym);
void SetEmulatePressRightKey(KeySym);
bool EmulateMouseAction(KeySym);
#endif
private:
LocalEvent();
void HandleMouseMotionEvent(const SDL_MouseMotionEvent & motion);
void HandleMouseButtonEvent(const SDL_MouseButtonEvent & button);
void HandleKeyboardEvent(SDL_KeyboardEvent &);
#if SDL_VERSION_ATLEAST(1, 3, 0)
static int GlobalFilterEvents(void *userdata, SDL_Event *event);
#else
static int GlobalFilterEvents(const SDL_Event *event);
#endif
enum flag_t
{
KEY_PRESSED = 0x0001,
MOUSE_MOTION = 0x0002,
MOUSE_PRESSED = 0x0004,
GLOBAL_FILTER = 0x0008,
CLICK_LEFT = 0x0010,
CLICK_RIGHT = 0x0020,
CLICK_MIDDLE = 0x0040,
TAP_MODE = 0x0080,
MOUSE_OFFSET = 0x0100,
CLOCK_ON = 0x0200
};
void SetModes(flag_t);
void ResetModes(flag_t);
u16 modes;
KeySym key_value;
u8 mouse_state;
u8 mouse_button;
Point mouse_st; // mouse offset for pocketpc
Point mouse_pl; // press left
Point mouse_pm; // press middle
Point mouse_pr; // press right
Point mouse_rl; // release left
Point mouse_rm; // release middle
Point mouse_rr; // release right
Point mouse_cu; // point cursor
void (*redraw_cursor_func)(u16, u16);
void (*keyboard_filter_func)(int, u16);
SDL::Time clock;
u32 clock_delay;
u8 loop_delay;
#ifdef WITHOUT_MOUSE
bool emulate_mouse;
KeySym emulate_mouse_up;
KeySym emulate_mouse_down;
KeySym emulate_mouse_left;
KeySym emulate_mouse_right;
u8 emulate_mouse_step;
KeySym emulate_press_left;
KeySym emulate_press_right;
#endif
};
#endif

View File

@@ -1,69 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include "midi.h"
u8 MIDI::UnpackDelta(const u8 *p, u32 & d)
{
const u8 *p2 = p;
d = 0;
while(*p2 & 0x80)
{
if(4 <= p2 - p)
{
std::cerr << "Event: unpack delta mistake" << std::endl;
break;
}
d |= 0x0000007F & static_cast<u32>(*p2);
d <<= 7;
++p2;
}
d += *p2;
return p2 - p + 1;
}
u8 MIDI::PackDelta(u8 *p, const u32 & d)
{
const u8 c1 = static_cast<char>(d & 0x0000007F);
const u8 c2 = static_cast<char>((d & 0x00003F80) >> 7);
const u8 c3 = static_cast<char>((d & 0x001FC000) >> 14);
const u8 c4 = static_cast<char>((d & 0x0FE00000) >> 21);
if(c4)
{ p[0] = c4 | 0x80; p[1] = c3 | 0x80; p[2] = c2 | 0x80; p[3] = c1; }
else
if(c3)
{ p[0] = c3 | 0x80; p[1] = c2 | 0x80; p[2] = c1; }
else
if(c2)
{ p[0] = c2 | 0x80; p[1] = c1; }
else
{ p[0] = c1; }
return (c4 ? 4 : (c3 ? 3 : (c2 ? 2 : 1)));
}

View File

@@ -1,34 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef MIDI_H
#define MIDI_H
#include "types.h"
namespace MIDI
{
u8 UnpackDelta(const u8 *p, u32 & d);
u8 PackDelta(u8 *p, const u32 & d);
}
#endif

View File

@@ -1,203 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <cstring>
#include <iostream>
#include <iomanip>
#include "midi_chunk.h"
using namespace MIDI;
Chunk::Chunk() : size(0), data(NULL)
{
memset(id, '\0', 4);
}
Chunk::Chunk(const char *i, const u32 s, const u8 *p) : size(s), data(NULL)
{
i ? memcpy(id, i, 4) : memset(id, '\0', 4);
if(size)
{
data = new u8[size];
if(p) memcpy(data, p, size); else memset(data, '\0', size);
}
}
Chunk::Chunk(std::istream & i) : size(0), data(NULL)
{
memset(id, '\0', 4);
Read(i);
}
Chunk::Chunk(const u8 *p) : size(0), data(NULL)
{
memset(id, '\0', 4);
Read(p);
}
Chunk::Chunk(const Chunk & c) : size(c.size), data(NULL)
{
c.id ? memcpy(id, c.id, 4) : memset(id, '\0', 4);
if(size)
{
data = new u8 [size];
memcpy(data, c.data, size);
}
}
Chunk::~Chunk()
{
if(data) delete [] data;
}
Chunk & Chunk::operator= (const Chunk & c)
{
if(data) delete [] data;
data = NULL;
c.id ? memcpy(id, c.id, 4) : memset(id, '\0', 4);
size = c.size;
if(size)
{
data = new u8 [size];
memcpy(data, c.data, size);
}
return *this;
}
bool Chunk::Read(std::istream & i)
{
if(i.fail()) return false;
i.read(id, 4);
i.read(reinterpret_cast<char *>(&size), 4);
SwapBE32(size);
if(data) delete [] data;
data = NULL;
if(size)
{
data = new u8 [size];
i.read(reinterpret_cast<char *>(data), size);
}
return true;
}
bool Chunk::Read(const std::vector<u8> & b)
{
if(8 > b.size()) return false;
memcpy(id, &b[0], 4);
size = ReadBE32(&b[4]);
if(data) delete [] data;
data = NULL;
if(size + 8 > b.size()) size = b.size() - 8;
if(size)
{
data = new u8 [size];
memcpy(data, &b[8], size);
}
return true;
}
bool Chunk::Read(const u8 *p)
{
if(NULL == p) return false;
memcpy(id, p, 4);
size = ReadBE32(&p[4]);
if(data) delete [] data;
data = NULL;
if(size)
{
data = new u8 [size];
memcpy(data, &p[8], size);
}
return true;
}
bool Chunk::Write(std::ostream & o) const
{
if(o.fail()) return false;
o.write(id, 4);
u32 x = size;
SwapBE32(x);
o.write(reinterpret_cast<char *>(&x), 4);
if(size && data) o.write(reinterpret_cast<char *>(data), size);
return true;
}
bool Chunk::Write(u8 *p) const
{
if(NULL == p) return false;
memcpy(p, id, 4);
WriteBE32(&p[4], size);
if(size && data) memcpy(&p[8], data, size);
return true;
}
void Chunk::Dump(void) const
{
std::cerr << "id: ";
std::cerr.write(id, 4);
std::cerr << std::endl << "size: " << std::dec << size << std::endl << "data: " << std::endl;
u8 endline = 0;
for(u32 ii = 0; ii < size; ++ii)
{
std::cerr << " 0x" << std::setw(2) << std::setfill('0') << std::hex << static_cast<u32>(static_cast<u8>(data[ii])) << ":";
++endline;
if(endline > 15)
{
endline = 0;
std::cerr << std::endl;
}
}
std::cerr << std::endl;
}

View File

@@ -1,60 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef MIDI_CHUNK_H
#define MIDI_CHUNK_H
#include <ostream>
#include <istream>
#include <vector>
#include "midi.h"
namespace MIDI
{
class Chunk
{
public:
char id[4];
u32 size;
u8* data;
Chunk();
Chunk(const char *i, const u32 s, const u8 *p = NULL);
Chunk(std::istream & i);
Chunk(const u8 *p);
Chunk(const Chunk & c);
~Chunk();
Chunk & operator= (const Chunk & c);
bool Write(std::ostream & o) const;
bool Write(u8 *p) const;
bool Read(std::istream & i);
bool Read(const u8 *p);
bool Read(const std::vector<u8> & b);
void Dump(void) const;
};
}
#endif

View File

@@ -1,146 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <cstring>
#include <iomanip>
#include <iostream>
#include "midi_event.h"
using namespace MIDI;
Event::Event() : delta(0), status(0), sp(0)
{
}
Event::Event(const u32 dl, const u8 st, const u32 sz, const u8 *p) : delta(dl), status(st), data(NULL)
{
if(sz)
{
data = new u8 [sz];
size = sz;
memcpy(data, p, size);
}
SetDelta(dl);
}
Event::Event(const Event & e)
{
delta = e.delta;
status = e.status;
data = NULL;
size = e.size;
if(size)
{
data = new u8 [size];
memcpy(data, e.data, size);
}
memcpy(pack, e.pack, 4);
sp = e.sp;
}
Event::~Event()
{
if(data) delete [] data;
}
Event & Event::operator= (const Event & e)
{
if(data) delete [] data;
delta = e.delta;
status = e.status;
data = NULL;
size = e.size;
if(size)
{
data = new u8 [size];
memcpy(data, e.data, size);
}
memcpy(pack, e.pack, 4);
sp = e.sp;
return *this;
}
void Event::SetDelta(const u32 dl)
{
sp = MIDI::PackDelta(pack, dl);
}
u32 Event::Size(void) const
{
return 1 + sp + size;
}
bool Event::Write(u8 *p) const
{
if(NULL == p) return false;
memcpy(p, pack, sp);
p+= sp;
*p = status;
if(size) memcpy(p + 1, data, size);
return true;
}
bool Event::Write(std::ostream & o) const
{
if(o.fail()) return false;
o.write(reinterpret_cast<const char*>(pack), sp);
o.write(&status, 1);
if(size) o.write(reinterpret_cast<const char*>(data), size);
return true;
}
void Event::Dump(void) const
{
std::cerr << std::hex << std::setfill('0') \
<< "[dl:0x" << std::setw(4) << delta \
<< ":st:0x" << std::setw(2) << static_cast<u16>(static_cast<u8>(status)) << ":dt";
u8 endline = 0;
for(u32 ii = 0; ii < size; ++ii)
{
std::cerr << " 0x" << std::setw(2) << std::setfill('0') << std::hex << static_cast<u32>(static_cast<u8>(data[ii])) << ":";
++endline;
if(endline > 15)
{
endline = 0;
std::cerr << std::endl;
}
}
std::cerr << "]" << std::endl;
}

View File

@@ -1,63 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef MIDI_EVENT_H
#define MIDI_EVENT_H
#include <ostream>
#include "midi.h"
namespace MIDI
{
class Event
{
public:
Event();
Event(const u32 dl, const u8 st, const u32 sz, const u8 *p);
Event(const Event &);
~Event();
Event & operator= (const Event &);
u32 Size(void) const;
u32 Delta(void) const { return delta; };
u8 Status(void) const { return status; };
void SetDelta(const u32 dl);
void Dump(void) const;
bool Write(u8 *p) const;
bool Write(std::ostream & o) const;
protected:
u32 delta;
char status;
u8* data;
u32 size;
u8 pack[4];
u8 sp;
};
}
#endif

View File

@@ -1,228 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <string>
#include <fstream>
#include <iostream>
#include "midi_chunk.h"
#include "midi_mid.h"
using namespace MIDI;
Mid::Mid()
{
}
Mid::Mid(const Mid & m) : mthd(m.mthd)
{
std::list<MTrk *>::const_iterator it1 = m.tracks.begin();
std::list<MTrk *>::const_iterator it2 = m.tracks.end();
for(; it1 != it2; ++it1) if(*it1) tracks.push_back(new MTrk(**it1));
}
Mid::~Mid()
{
if(tracks.size())
{
std::list<MTrk *>::const_iterator it1 = tracks.begin();
std::list<MTrk *>::const_iterator it2 = tracks.end();
for(; it1 != it2; ++it1) if(*it1) delete *it1;
}
}
Mid & Mid::operator= (const Mid & m)
{
mthd = m.mthd;
if(tracks.size())
{
std::list<MTrk *>::const_iterator it1 = tracks.begin();
std::list<MTrk *>::const_iterator it2 = tracks.end();
for(; it1 != it2; ++it1) delete *it1;
}
std::list<MTrk *>::const_iterator it1 = m.tracks.begin();
std::list<MTrk *>::const_iterator it2 = m.tracks.end();
for(; it1 != it2; ++it1) if(*it1) tracks.push_back(new MTrk(**it1));
return *this;
}
bool Mid::Read(const std::vector<u8> & body)
{
mthd.Read(body);
if(! mthd.isValid())
{
std::cerr << "Mid::Read: " << "error format" << std::endl;
return false;
}
const u32 count = mthd.Tracks();
const u8 *ptr = &body[mthd.Size()];
for(u16 ii = 0; ii < count; ++ii)
{
if(ptr >= &body[0] + body.size())
{
std::cerr << "Mid::Read: " << "error read chunk, total: " << count << ", current: " << ii << std::endl;
return false;
}
const Chunk chunk(ptr);
if(0 == memcmp(ID_MTRK, chunk.id, 4)) tracks.push_back(new MTrk(chunk.data, chunk.size));
else --ii;
ptr += 8 + chunk.size;
}
return true;
}
bool Mid::Read(const std::string & filename)
{
std::ifstream fd(filename.c_str(), std::ios::binary);
if(!fd.is_open())
{
std::cerr << "Mid::Read: " << "error read: " << filename << std::endl;
return false;
}
mthd.Read(fd);
if(! mthd.isValid())
{
std::cerr << "Mid::Read: " << "error format: " << filename << std::endl;
return false;
}
const u32 count = mthd.Tracks();
for(u16 ii = 0; ii < count; ++ii)
{
if(fd.fail())
{
std::cerr << "Mid::Read: " << "error read chunk, total: " << count << ", current: " << ii << std::endl;
return false;
}
const Chunk chunk(fd);
if(0 == memcmp(ID_MTRK, chunk.id, 4)) tracks.push_back(new MTrk(chunk.data, chunk.size));
else --ii;
}
fd.close();
return true;
}
u32 Mid::Size(void) const
{
u32 total = mthd.Size();
if(tracks.size())
{
std::list<MTrk *>::const_iterator it1 = tracks.begin();
std::list<MTrk *>::const_iterator it2 = tracks.end();
for(; it1 != it2; ++it1) if(*it1) total += (*it1)->Size();
}
return total;
}
bool Mid::Write(std::vector<u8> & body)
{
body.resize(Size());
u8 *ptr = &body[0];
mthd.Write(ptr);
ptr += mthd.Size();
if(tracks.size())
{
std::list<MTrk *>::const_iterator it1 = tracks.begin();
std::list<MTrk *>::const_iterator it2 = tracks.end();
for(; it1 != it2; ++it1) if(*it1){ (*it1)->Write(ptr); ptr += (*it1)->Size(); }
}
return true;
}
bool Mid::Write(const std::string & filename)
{
std::ofstream fd(filename.c_str(), std::ios::binary);
if(!fd.is_open())
{
std::cerr << "Mid::Write: " << "error write: " << filename << std::endl;
return false;
}
mthd.Write(fd);
if(tracks.size())
{
std::list<MTrk *>::const_iterator it1 = tracks.begin();
std::list<MTrk *>::const_iterator it2 = tracks.end();
for(; it1 != it2; ++it1) if(*it1) (*it1)->Write(fd);
}
fd.close();
return true;
}
void Mid::Dump(void) const
{
mthd.Dump();
if(tracks.size())
{
std::list<MTrk *>::const_iterator it1 = tracks.begin();
std::list<MTrk *>::const_iterator it2 = tracks.end();
for(; it1 != it2; ++it1) if(*it1) (*it1)->Dump();
}
}
void Mid::AddTrack(MTrk & track)
{
tracks.push_back(new MTrk(track));
mthd.SetTracks(mthd.Tracks() + 1);
}

View File

@@ -1,64 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef MIDI_MID_H
#define MIDI_MID_H
#include <vector>
#include <list>
#include "midi.h"
#include "midi_mthd.h"
#include "midi_mtrk.h"
namespace MIDI
{
class Mid
{
public:
Mid();
Mid(const Mid & m);
~Mid();
Mid & operator= (const Mid & m);
bool Read(const std::string & filename);
bool Read(const std::vector<u8> & body);
bool Write(const std::string & filename);
bool Write(std::vector<u8> & body);
u32 Size(void) const;
void SetFormat(const u16 f){ mthd.SetFormat(f); };
void SetTracks(const u16 t){ mthd.SetTracks(t); };
void SetPPQN(const u16 p){ mthd.SetPPQN(p); };
void AddTrack(MTrk & track);
void Dump(void) const;
private:
MThd mthd;
std::list<MTrk *> tracks;
};
}
#endif

View File

@@ -1,46 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include "midi_mthd.h"
using namespace MIDI;
void MThd::Dump(void) const
{
std::cerr << "[MThd] format: " << Format() << ", tracks: " << Tracks() << ", ppqn: " << PPQN() << std::endl;
}
void MThd::SetFormat(const u16 f)
{
WriteBE16(reinterpret_cast<u8*>(&data[0]), f);
}
void MThd::SetTracks(const u16 t)
{
WriteBE16(reinterpret_cast<u8*>(&data[2]), t);
}
void MThd::SetPPQN(const u16 p)
{
WriteBE16(reinterpret_cast<u8*>(&data[4]), p);
}

View File

@@ -1,63 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef MIDI_MTHD_H
#define MIDI_MTHD_H
#include <vector>
#include <istream>
#include <cstring>
#include "midi.h"
#include "midi_chunk.h"
#define ID_MTHD "MThd"
namespace MIDI
{
class MThd : protected Chunk
{
public:
MThd() : Chunk(ID_MTHD, 6) {};
MThd(const u8 *p, const u32 s) : Chunk(ID_MTHD, s, p) {};
MThd(std::istream & i) : Chunk(i) {};
bool isValid(void) const{ return 0 == memcmp(Chunk::id, ID_MTHD, 4); };
bool Read(std::istream & is){ return Chunk::Read(is); };
bool Read(const std::vector<u8> & b){ return Chunk::Read(b); };
bool Write(std::ostream & os) const{ return Chunk::Write(os); };
bool Write(u8* b) const{ return Chunk::Write(b); };
void SetFormat(const u16 f);
void SetTracks(const u16 t);
void SetPPQN(const u16 p);
const u8* Data(void) const{ return Chunk::data; };
u32 Size(void) const{ return 8 + size; };
u16 Format(void) const{ return ReadBE16(reinterpret_cast<const u8*>(&data[0])); };
u16 Tracks(void) const{ return ReadBE16(reinterpret_cast<const u8*>(&data[2])); };
u16 PPQN(void) const{ return ReadBE16(reinterpret_cast<const u8*>(&data[4])); };
void Dump(void) const;
};
}
#endif

View File

@@ -1,348 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iomanip>
#include <functional>
#include <algorithm>
#include <iostream>
#include <string.h>
#include "midi_mtrk.h"
using namespace MIDI;
struct meta_t
{
meta_t() : command(0), quantity(0), duration(0){}
meta_t(u8 c, u8 q, u32 d) : command(c), quantity(q), duration(d){}
bool operator< (const meta_t & m) const{ return duration < m.duration; }
void decrease_duration(u32 delta) { duration -= delta; }
u8 command;
u8 quantity;
u32 duration;
};
MTrk::MTrk(const u8 *p, const u32 s)
{
const u8 *ptr = p;
bool end = false;
while(ptr && !end && ptr < (p + s))
{
u32 delta = 0;
const u8 s = MIDI::UnpackDelta(ptr, delta);
ptr += s;
const u8 status = *ptr;
ptr += 1;
switch(status >> 4)
{
// meta
case 0x0F:
{
u32 size = 0;
const u8 s = MIDI::UnpackDelta(ptr + 1, size);
if(0xFF == status && 0x2F == *ptr)
{
end = true;
events.push_back(new Event(delta, status, 1 + s + size, ptr));
}
ptr += 1 + s + size;
}
break;
// note off
case 0x08:
// note on
case 0x09:
// key pressure
case 0x0A:
// control change
case 0x0B:
// pitch bend
case 0x0E:
{
events.push_back(new Event(delta, status, 2, ptr));
ptr += 2;
}
break;
// program change
case 0x0C:
// chanel pressure
case 0x0D:
{
events.push_back(new Event(delta, status, 1, ptr));
ptr += 1;
}
break;
// unused command
default:
end = true;
CloseEvents();
std::cerr << "unknown st: 0x" << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(status) << ", ln: " << static_cast<int>(p + s - ptr) << std::endl;
break;
}
}
}
MTrk::MTrk(const MTrk & t)
{
std::list<Event *>::const_iterator it1 = t.events.begin();
std::list<Event *>::const_iterator it2 = t.events.end();
for(; it1 != it2; ++it1) if(*it1) events.push_back(new Event(**it1));
}
MTrk::~MTrk()
{
if(events.size())
{
std::list<Event *>::const_iterator it1 = events.begin();
std::list<Event *>::const_iterator it2 = events.end();
for(; it1 != it2; ++it1) if(*it1) delete *it1;
}
}
u32 MTrk::Size(void) const
{
u32 result = 8; // id + size
std::list<Event *>::const_iterator it1 = events.begin();
std::list<Event *>::const_iterator it2 = events.end();
for(; it1 != it2; ++it1) if(*it1) result += (*it1)->Size();
return result;
}
bool MTrk::Write(std::ostream & o) const
{
if(o.fail()) return false;
o.write(ID_MTRK, 4);
u32 size = 0;
std::list<Event *>::const_iterator it1 = events.begin();
std::list<Event *>::const_iterator it2 = events.end();
for(; it1 != it2; ++it1) if(*it1) size += (*it1)->Size();
u32 x = size;
SwapBE32(x);
o.write(reinterpret_cast<char *>(&x), 4);
if(events.size())
{
it1 = events.begin();
it2 = events.end();
for(; it1 != it2; ++it1) if(*it1) (*it1)->Write(o);
}
return true;
}
bool MTrk::Write(u8 *p) const
{
if(NULL == p) return false;
memcpy(p, ID_MTRK, 4);
p+= 4;
u32 size = 0;
std::list<Event *>::const_iterator it1 = events.begin();
std::list<Event *>::const_iterator it2 = events.end();
for(; it1 != it2; ++it1) if(*it1) size += (*it1)->Size();
u32 x = size;
WriteBE32(p, x);
p+= 4;
if(events.size())
{
it1 = events.begin();
it2 = events.end();
for(; it1 != it2; ++it1) if(*it1){ (*it1)->Write(p); p += (*it1)->Size(); }
}
return true;
}
void MTrk::AddEvent(const Event & e)
{
events.push_back(new Event(e));
}
void MTrk::CloseEvents(void)
{
events.push_back(new Event(0, 0xFF, 2, reinterpret_cast<const u8*>("\057\000")));
}
void MTrk::Dump(void) const
{
std::cerr << "[MTrk]\n";
if(events.size())
{
std::list<Event *>::const_iterator it1 = events.begin();
std::list<Event *>::const_iterator it2 = events.end();
for(; it1 != it2; ++it1) if(*it1) (*it1)->Dump();
}
std::cerr << std::endl;
}
void MTrk::ImportXmiEVNT(const Chunk & evnt)
{
const u8 *ptr = evnt.data;
u8 buf[2];
u32 delta = 0;
u32 delta2 = 0;
std::list<meta_t> notesoff;
std::list<meta_t>::iterator it1, it2;
while(ptr && ptr < (evnt.data + evnt.size))
{
// insert event: note off
if(delta)
{
// sort duration
notesoff.sort();
it1 = notesoff.begin();
it2 = notesoff.end();
delta2 = 0;
// apply delta
for(; it1 != it2; ++it1)
{
if((*it1).duration <= delta)
{
buf[0] = (*it1).quantity;
buf[1] = 0x7F;
// note off
events.push_back(new Event((*it1).duration - delta2, (*it1).command, 2, buf));
delta2 += ((*it1).duration - delta2);
}
}
// remove end notes
while(notesoff.size() && notesoff.front().duration <= delta)
notesoff.pop_front();
// fixed delta
if(delta2) delta -= delta2;
// decrease duration
std::for_each(notesoff.begin(), notesoff.end(), std::bind2nd(std::mem_fun_ref(&meta_t::decrease_duration), delta));
}
// interval
if(*ptr < 128)
{
delta += *ptr;
++ptr;
}
else
// command
{
// end
if(0xFF == *ptr && 0x2F == *(ptr + 1))
{
events.push_back(new Event(delta, *ptr, 2, ptr + 1));
break;
}
else
switch(*ptr >> 4)
{
// meta
case 0x0F:
{
u32 size = 0;
size += 1 + MIDI::UnpackDelta(ptr + 2, size);
ptr += size + 1;
delta = 0;
}
break;
// key pressure
case 0x0A:
// control change
case 0x0B:
// pitch bend
case 0x0E:
{
events.push_back(new Event(delta, *ptr, 2, ptr + 1));
ptr += 3;
delta = 0;
}
break;
// note off
case 0x08:
{
events.push_back(new Event(delta, *ptr, 2, ptr + 1));
u32 duration = 0;
const u8 s = MIDI::UnpackDelta(ptr + 3, duration);
notesoff.push_back(meta_t(*ptr - 0x10, *(ptr + 1), duration));
ptr += 3 + s;
delta = 0;
}
break;
// note on
case 0x09:
{
events.push_back(new Event(delta, *ptr, 2, ptr + 1));
u32 duration = 0;
const u8 s = MIDI::UnpackDelta(ptr + 3, duration);
notesoff.push_back(meta_t(*ptr - 0x10, *(ptr + 1), duration));
ptr += 3 + s;
delta = 0;
}
break;
// program change
case 0x0C:
// chanel pressure
case 0x0D:
{
events.push_back(new Event(delta, *ptr, 1, ptr + 1));
ptr += 2;
delta = 0;
}
break;
// unused command
default:
CloseEvents();
std::cerr << "unknown st: 0x" << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(*ptr) << ", ln: " << static_cast<int>(evnt.data + evnt.size - ptr) << std::endl;
break;
}
}
}
}

View File

@@ -1,62 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef MIDI_TRACK_H
#define MIDI_TRACK_H
#include <list>
#include <ostream>
#include "midi.h"
#include "midi_chunk.h"
#include "midi_event.h"
#define ID_MTRK "MTrk"
namespace MIDI
{
u8 UnpackDelta(const char *p, u32 & d);
u8 PackDelta(char *p, const u32 & d);
class MTrk
{
public:
MTrk() {};
MTrk(const u8 *p, const u32 s);
MTrk(const MTrk & t);
~MTrk();
bool Write(std::ostream & o) const;
bool Write(u8 *p) const;
u32 Size(void) const;
void AddEvent(const Event & e);
void ImportXmiEVNT(const Chunk & c);
void CloseEvents(void);
void Dump(void) const;
private:
std::list<Event *> events;
};
}
#endif

View File

@@ -1,138 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <fstream>
#include <iostream>
#include <cstring>
#include <vector>
#include "midi_xmi.h"
#define ID_FORM "FORM"
#define ID_CAT "CAT "
#define ID_XMID "XMID"
#define ID_TIMB "TIMB"
#define ID_EVNT "EVNT"
using namespace MIDI;
Xmi::Xmi()
{
}
bool Xmi::Read(const std::vector<u8> & body)
{
if(0 == body.size())
{
std::cerr << "Xmi: " << "incorrect size" << std::endl;
return false;
}
const u8 *ptr = &body[0];
if(memcmp(ID_FORM, ptr, 4))
{
std::cerr << "Xmi: " << "incorrect id: " << ID_FORM << std::endl;
return false;
}
head.Read(ptr);
ptr += 8 + head.size;
if(memcmp(ID_CAT, ptr, 4))
{
std::cerr << "Xmi: " << "incorrect id: " << ID_CAT<< std::endl;
return false;
}
ptr += 8;
if(memcmp(ID_XMID, ptr, 4))
{
std::cerr << "Xmi: " << "incorrect cat id: " << ID_XMID << std::endl;
return false;
}
ptr += 4;
if(memcmp(ID_FORM, ptr, 4))
{
std::cerr << "Xmi: " << "incorrect xmid id: " << ID_FORM << std::endl;
return false;
}
else
ptr += 8;
if(memcmp(ID_XMID, ptr, 4))
{
std::cerr << "Xmi: " << "incorrect form id: " << ID_XMID << std::endl;
return false;
}
ptr += 4;
if(memcmp(ID_TIMB, ptr, 4))
{
std::cerr << "Xmi: " << "incorrect id: " << ID_TIMB << std::endl;
return false;
}
timb.Read(ptr);
ptr += 8 + timb.size;
if(memcmp(ID_EVNT, ptr, 4))
{
std::cerr << "Xmi: " << "incorrect id: " << ID_EVNT << std::endl;
return false;
}
evnt.Read(ptr);
return true;
}
bool Xmi::Read(const std::string & filename)
{
std::ifstream fd(filename.c_str(), std::ios::binary);
if(!fd.is_open())
{
std::cerr << "Xmi: " << "error read: " << filename.c_str() << std::endl;
return false;
}
fd.seekg(0, std::ios_base::end);
const u32 size = fd.tellg();
fd.seekg(0, std::ios_base::beg);
std::vector<u8> body(size);
fd.read(reinterpret_cast<char*>(&body[0]), size);
fd.close();
return Read(body);
}
void Xmi::Dump(void) const
{
head.Dump();
timb.Dump();
evnt.Dump();
}

View File

@@ -1,52 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef MIDI_XMI_H
#define MIDI_XMI_H
#include <list>
#include "midi.h"
#include "midi_chunk.h"
namespace MIDI
{
class Xmi
{
public:
Xmi();
bool Read(const std::string & filename);
bool Read(const std::vector<u8> & body);
const Chunk & TIMB(void) const { return timb; }
const Chunk & EVNT(void) const { return evnt; }
void Dump(void) const;
private:
Chunk head;
Chunk timb;
Chunk evnt;
};
}
#endif

View File

@@ -1,87 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "SDL.h"
#include "surface.h"
#include "error.h"
#include "palette_h2.h"
#include "palette.h"
#define PALETTE_SIZE 255
Palette::Palette()
{
sdlpal = new SDL_Palette;
sdlpal->ncolors = PALETTE_SIZE;
sdlpal->colors = new SDL_Color[PALETTE_SIZE];
pal = new u32 [PALETTE_SIZE];
Surface sfa;
sfa.CreateSurface(1, 1, Surface::GetDefaultDepth(), SDL_SWSURFACE|SDL_SRCALPHA);
const unsigned char *p = kb_pal;
for(u16 ii = 0; ii < PALETTE_SIZE; ++ii)
{
sdlpal->colors[ii].r = *p++;
sdlpal->colors[ii].g = *p++;
sdlpal->colors[ii].b = *p++;
sdlpal->colors[ii].r <<= 2;
sdlpal->colors[ii].g <<= 2;
sdlpal->colors[ii].b <<= 2;
pal[ii] = SDL_MapRGBA(sfa.surface->format, sdlpal->colors[ii].r, sdlpal->colors[ii].g, sdlpal->colors[ii].b, 0xFF);
}
}
Palette::~Palette()
{
if(sdlpal)
{
if(sdlpal->colors) delete [] sdlpal->colors;
delete sdlpal;
}
if(pal) delete [] pal;
}
Palette & Palette::Get(void)
{
static Palette pal_cache;
return pal_cache;
}
u16 Palette::Size(void) const
{
return PALETTE_SIZE;
}
u32 Palette::GetColor(u16 index) const
{
return index < PALETTE_SIZE ? pal[index] : 0;
}
const SDL_Palette * Palette::SDLPalette(void) const
{
return sdlpal;
}

View File

@@ -1,49 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2PALETTE_H
#define H2PALETTE_H
#include "types.h"
struct SDL_Palette;
struct SDL_Color;
class Palette
{
public:
~Palette();
static Palette & Get(void);
u16 Size(void) const;
u32 GetColor(u16) const;
const SDL_Palette * SDLPalette(void) const;
private:
Palette();
u32* pal;
SDL_Palette *sdlpal;
};
#endif

View File

@@ -1,55 +0,0 @@
#ifndef H2KBPAL_H
#define H2KBPAL_H
static const unsigned char kb_pal[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x3f,
0x3f, 0x3c, 0x3c, 0x3c, 0x3a, 0x3a, 0x3a, 0x37, 0x37, 0x37, 0x35, 0x35, 0x35, 0x32, 0x32, 0x32,
0x30, 0x30, 0x30, 0x2d, 0x2d, 0x2d, 0x2b, 0x2b, 0x2b, 0x29, 0x29, 0x29, 0x26, 0x26, 0x26, 0x24,
0x24, 0x24, 0x21, 0x21, 0x21, 0x1f, 0x1f, 0x1f, 0x1c, 0x1c, 0x1c, 0x1a, 0x1a, 0x1a, 0x17, 0x17,
0x17, 0x15, 0x15, 0x15, 0x12, 0x12, 0x12, 0x10, 0x10, 0x10, 0x0e, 0x0e, 0x0e, 0x0b, 0x0b, 0x0b,
0x09, 0x09, 0x09, 0x06, 0x06, 0x06, 0x04, 0x04, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x3f,
0x3b, 0x37, 0x3c, 0x37, 0x32, 0x3a, 0x34, 0x2e, 0x38, 0x31, 0x2a, 0x36, 0x2e, 0x26, 0x34, 0x2a,
0x22, 0x32, 0x28, 0x1e, 0x30, 0x25, 0x1b, 0x2e, 0x22, 0x18, 0x2b, 0x1f, 0x15, 0x29, 0x1c, 0x12,
0x27, 0x1a, 0x0f, 0x25, 0x18, 0x0d, 0x23, 0x15, 0x0b, 0x21, 0x13, 0x08, 0x1f, 0x11, 0x07, 0x1d,
0x0f, 0x05, 0x1a, 0x0d, 0x04, 0x18, 0x0c, 0x03, 0x16, 0x0a, 0x02, 0x14, 0x09, 0x01, 0x12, 0x07,
0x01, 0x0f, 0x06, 0x00, 0x0d, 0x05, 0x00, 0x0b, 0x04, 0x00, 0x09, 0x03, 0x00, 0x30, 0x33, 0x3f,
0x2b, 0x2e, 0x3c, 0x26, 0x2a, 0x3a, 0x22, 0x26, 0x38, 0x1e, 0x22, 0x36, 0x1a, 0x1e, 0x34, 0x16,
0x1a, 0x31, 0x13, 0x16, 0x2f, 0x10, 0x13, 0x2d, 0x0d, 0x10, 0x2b, 0x0a, 0x0d, 0x29, 0x08, 0x0c,
0x26, 0x07, 0x0a, 0x24, 0x05, 0x09, 0x22, 0x04, 0x08, 0x20, 0x03, 0x07, 0x1e, 0x02, 0x06, 0x1c,
0x01, 0x05, 0x19, 0x01, 0x05, 0x17, 0x00, 0x04, 0x15, 0x00, 0x03, 0x13, 0x00, 0x03, 0x11, 0x2b,
0x38, 0x27, 0x27, 0x35, 0x23, 0x24, 0x33, 0x20, 0x20, 0x30, 0x1c, 0x1d, 0x2e, 0x19, 0x1a, 0x2c,
0x17, 0x17, 0x29, 0x14, 0x14, 0x27, 0x11, 0x12, 0x24, 0x0f, 0x0f, 0x22, 0x0c, 0x0d, 0x1f, 0x0a,
0x0b, 0x1d, 0x09, 0x09, 0x1b, 0x07, 0x08, 0x19, 0x06, 0x06, 0x17, 0x05, 0x05, 0x15, 0x03, 0x03,
0x13, 0x02, 0x02, 0x10, 0x01, 0x01, 0x0e, 0x01, 0x01, 0x0c, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x08,
0x00, 0x00, 0x06, 0x00, 0x3f, 0x3d, 0x34, 0x3e, 0x3a, 0x2b, 0x3d, 0x38, 0x23, 0x3c, 0x37, 0x1b,
0x3b, 0x35, 0x14, 0x3a, 0x33, 0x0d, 0x39, 0x32, 0x05, 0x38, 0x31, 0x00, 0x36, 0x2f, 0x08, 0x34,
0x2c, 0x07, 0x32, 0x28, 0x06, 0x2f, 0x26, 0x06, 0x2d, 0x23, 0x06, 0x2a, 0x1f, 0x05, 0x27, 0x1c,
0x04, 0x25, 0x19, 0x03, 0x22, 0x16, 0x03, 0x1f, 0x13, 0x02, 0x1d, 0x11, 0x02, 0x1a, 0x0f, 0x00,
0x18, 0x0c, 0x00, 0x15, 0x0a, 0x00, 0x13, 0x08, 0x00, 0x39, 0x33, 0x3e, 0x36, 0x2f, 0x3b, 0x32,
0x2a, 0x39, 0x30, 0x27, 0x36, 0x2d, 0x23, 0x34, 0x2a, 0x1f, 0x31, 0x27, 0x1c, 0x2f, 0x24, 0x19,
0x2d, 0x21, 0x16, 0x2a, 0x1e, 0x13, 0x28, 0x1c, 0x11, 0x25, 0x19, 0x0e, 0x23, 0x17, 0x0c, 0x20,
0x14, 0x0a, 0x1e, 0x12, 0x08, 0x1b, 0x10, 0x06, 0x19, 0x0e, 0x05, 0x17, 0x0b, 0x02, 0x14, 0x08,
0x01, 0x11, 0x06, 0x00, 0x0e, 0x04, 0x00, 0x0b, 0x2d, 0x3d, 0x3f, 0x2a, 0x3a, 0x3c, 0x28, 0x38,
0x3a, 0x25, 0x36, 0x38, 0x22, 0x33, 0x35, 0x20, 0x31, 0x33, 0x1e, 0x2e, 0x31, 0x1c, 0x2c, 0x2f,
0x19, 0x2a, 0x2c, 0x17, 0x27, 0x2a, 0x16, 0x25, 0x28, 0x14, 0x23, 0x25, 0x12, 0x20, 0x23, 0x10,
0x1d, 0x20, 0x0e, 0x1a, 0x1d, 0x0c, 0x18, 0x1b, 0x0a, 0x15, 0x18, 0x08, 0x13, 0x16, 0x07, 0x10,
0x13, 0x05, 0x0e, 0x10, 0x04, 0x0b, 0x0e, 0x03, 0x09, 0x0b, 0x02, 0x07, 0x09, 0x3f, 0x39, 0x39,
0x3d, 0x34, 0x34, 0x3c, 0x2f, 0x2f, 0x3a, 0x2b, 0x2b, 0x39, 0x27, 0x27, 0x37, 0x23, 0x23, 0x36,
0x1f, 0x1f, 0x34, 0x1b, 0x1b, 0x33, 0x17, 0x17, 0x31, 0x14, 0x14, 0x30, 0x11, 0x11, 0x2f, 0x0e,
0x0e, 0x2e, 0x0b, 0x0b, 0x2d, 0x09, 0x09, 0x2a, 0x08, 0x08, 0x27, 0x06, 0x06, 0x24, 0x04, 0x04,
0x21, 0x03, 0x03, 0x1e, 0x02, 0x02, 0x1b, 0x01, 0x01, 0x18, 0x00, 0x00, 0x15, 0x00, 0x00, 0x12,
0x00, 0x00, 0x3f, 0x39, 0x27, 0x3e, 0x36, 0x23, 0x3d, 0x34, 0x1f, 0x3c, 0x31, 0x1c, 0x3b, 0x2e,
0x18, 0x3a, 0x2b, 0x14, 0x39, 0x28, 0x11, 0x38, 0x24, 0x0e, 0x38, 0x21, 0x0b, 0x33, 0x1d, 0x08,
0x2e, 0x19, 0x06, 0x29, 0x16, 0x04, 0x25, 0x12, 0x02, 0x20, 0x0f, 0x01, 0x1b, 0x0c, 0x00, 0x17,
0x0a, 0x00, 0x3f, 0x16, 0x03, 0x37, 0x0d, 0x01, 0x30, 0x05, 0x00, 0x29, 0x00, 0x00, 0x3f, 0x3f,
0x00, 0x3f, 0x33, 0x00, 0x30, 0x23, 0x00, 0x23, 0x12, 0x00, 0x29, 0x34, 0x00, 0x25, 0x2f, 0x00,
0x21, 0x2b, 0x00, 0x1e, 0x27, 0x01, 0x1a, 0x23, 0x01, 0x17, 0x1e, 0x01, 0x13, 0x1a, 0x01, 0x10,
0x16, 0x01, 0x0d, 0x12, 0x01, 0x0a, 0x1e, 0x34, 0x06, 0x1a, 0x31, 0x01, 0x12, 0x2d, 0x00, 0x0e,
0x2b, 0x03, 0x15, 0x2f, 0x00, 0x0e, 0x2b, 0x00, 0x10, 0x2d, 0x21, 0x38, 0x3f, 0x00, 0x26, 0x3f,
0x00, 0x14, 0x39, 0x00, 0x00, 0x29, 0x23, 0x23, 0x2f, 0x1c, 0x1c, 0x27, 0x15, 0x15, 0x1f, 0x0f,
0x0f, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#endif

View File

@@ -1,85 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
#include "rand.h"
void Rand::Init(void){ std::srand((u32) std::time(0)); }
u32 Rand::Get(u32 min, u32 max){ return max ? min + Get(max - min) : static_cast<u32>((min + 1) * (std::rand() / (RAND_MAX + 1.0))); }
Rand::Queue::Queue(u32 size)
{
reserve(size);
}
void Rand::Queue::Reset(void)
{
clear();
}
void Rand::Queue::Push(s32 value, u32 percent)
{
if(percent)
push_back(std::make_pair(value, percent));
}
size_t Rand::Queue::Size(void) const
{
return size();
}
s32 Rand::Queue::Get(void)
{
std::vector<ValuePercent>::iterator it;
// get max
it = begin();
u32 max = 0;
for(; it != end(); ++it) max += (*it).second;
// set weight (from 100)
it = begin();
for(; it != end(); ++it) (*it).second = 100 * (*it).second / max;
// get max
max = 0;
it = begin();
for(; it != end(); ++it) max += (*it).second;
u8 rand = Rand::Get(max);
u8 amount = 0;
it = begin();
for(; it != end(); ++it)
{
amount += (*it).second;
if(rand <= amount) return (*it).first;
}
std::cerr << "Rand::Queue::Get:" << " weight not found, return 0" << std::endl;
return 0;
}

View File

@@ -1,69 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2RAND_H
#define H2RAND_H
#include <vector>
#include <list>
#include <utility>
#include "types.h"
namespace Rand
{
void Init(void);
u32 Get(u32 min, u32 max = 0);
template< typename T > const T * Get(const std::vector< T > & vec)
{
if(vec.empty()) return NULL;
return & vec[Rand::Get(vec.size() - 1)];
}
template< typename T > const T * Get(const std::list< T > & list)
{
if(list.empty()) return NULL;
u32 index1 = Rand::Get(list.size() - 1);
u32 index2 = 0;
typename std::list<T>::const_iterator it = list.begin();
for(; it != list.end(); ++it) if(index1 == index2++) break;
return & (*it);
}
typedef std::pair<s32, u32> ValuePercent;
class Queue : private std::vector<ValuePercent>
{
public:
Queue(u32 size = 0);
void Reset(void);
void Push(s32, u32);
size_t Size(void) const;
s32 Get(void);
};
}
#endif

View File

@@ -1,165 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "rect.h"
Point::Point(s16 px, s16 py) : x(px), y(py)
{
}
bool Point::operator== (const Point & pt) const
{
return (x == pt.x && y == pt.y);
}
bool Point::operator!= (const Point & pt) const
{
return !(*this == pt);
}
Point & Point::operator+=(const Point & pt)
{
x += pt.x;
y += pt.y;
return *this;
}
Point & Point::operator-=(const Point & pt)
{
x -= pt.x;
y -= pt.y;
return *this;
}
Point operator+(const Point& pt1, const Point& pt2)
{
return Point(pt1.x + pt2.x, pt1.y + pt2.y);
}
Point operator-(const Point& pt1, const Point& pt2)
{
return Point(pt1.x - pt2.x, pt1.y - pt2.y);
}
Size::Size(u16 sw, u16 sh) : w(sw), h(sh)
{
}
bool Size::operator== (const Size & sz) const
{
return (w == sz.w && h == sz.h);
}
bool Size::operator!= (const Size & sz) const
{
return !(*this == sz);
}
bool Size::isEmpty(void) const
{
return 0 == w && 0 == h;
}
Rect::Rect(s16 rx, s16 ry, u16 rw, u16 rh) : Point(rx, ry), Size(rw, rh)
{
}
Rect::Rect(const SDL_Rect & rt) : Point(rt.x, rt.y), Size(rt.w, rt.h)
{
}
Rect::Rect(const Point & pt, u16 rw, u16 rh) : Point(pt), Size(rw, rh)
{
}
Rect::Rect(const Point & pt, const Size & sz) : Point(pt), Size(sz)
{
}
Rect::Rect(const Rect & rt1, const Rect & rt2)
{
x = rt1.x < rt2.x ? rt1.x : rt2.x;
y = rt1.y < rt2.y ? rt1.y : rt2.y;
w = rt1.x + rt1.w > rt2.x + rt2.w ? rt1.x + rt1.w - x : rt2.x + rt2.w - x;
h = rt1.y + rt1.h > rt2.y + rt2.h ? rt1.y + rt1.h - y : rt2.y + rt2.h - y;
}
Rect::Rect(const std::vector<Rect> & vect)
{
int x1 = 32766;
int y1 = 32766;
int x2 = -32766;
int y2 = -32766;
std::vector<Rect>::const_iterator it = vect.begin();
for(; it != vect.end(); ++it)
{
if((*it).x < x1) x1 = (*it).x;
if((*it).y < y1) y1 = (*it).y;
if((*it).x + (*it).w > x2) x2 = (*it).x + (*it).w;
if((*it).y + (*it).h > y2) y2 = (*it).y + (*it).h;
}
x = x1;
y = y1;
w = x2 - x1;
h = y2 - y1;
}
Rect & Rect::operator= (const Point & pt)
{
x = pt.x;
y = pt.y;
return *this;
}
bool Rect::operator== (const Rect & rt) const
{
return (x == rt.x && y == rt.y && w == rt.w && h == rt.h);
}
bool Rect::operator!= (const Rect & rt) const
{
return !(*this == rt);
}
bool Rect::operator& (const Point & pt) const
{
return !(pt.x < x || pt.y < y || pt.x >= (x + w) || pt.y >= (y + h));
}
bool Rect::operator& (const Rect & rt) const
{
return
((rt.x >= x && rt.x < x + w) ||
(rt.x + rt.w >= x && rt.x + rt.w < x + w) ||
(x >= rt.x && x < rt.x + rt.w) ||
(x + w >= rt.x && x + w < rt.x + rt.w)) &&
((rt.y >= y && rt.y < y + h) ||
(rt.y + rt.h >= y && rt.y + rt.h < y + h) ||
(y >= rt.y && y < rt.y + rt.h) ||
(y + h >= rt.y && y + h < rt.y + rt.h));
}

View File

@@ -1,83 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2RECT_H
#define H2RECT_H
#include <vector>
#include <functional>
#include "types.h"
struct Point
{
s16 x, y;
Point(s16 px = 0, s16 py = 0);
bool operator== (const Point & pt) const;
bool operator!= (const Point & pt) const;
Point & operator+=(const Point & pt);
Point & operator-=(const Point & pt);
};
Point operator+(const Point& pt1, const Point& pt2);
Point operator-(const Point& pt1, const Point& pt2);
struct Size
{
u16 w, h;
Size(u16 sw = 0, u16 sh = 0);
bool isEmpty(void) const;
bool operator== (const Size & sz) const;
bool operator!= (const Size & sz) const;
};
struct Rect : Point, Size
{
Rect(s16 rx = -1, s16 ry = -1, u16 rw = 0, u16 rh = 0);
Rect(const SDL_Rect & rt);
Rect(const Point & pt, u16 rw, u16 rh);
Rect(const Point & pt, const Size & sz);
Rect(const Rect & rt1, const Rect & rt2);
Rect(const std::vector<Rect> & vect);
Rect & operator= (const Point & pt);
bool operator== (const Rect & rt) const;
bool operator!= (const Rect & rt) const;
// rect include point
bool operator& (const Point & pt) const;
// rect intersects rect
bool operator& (const Rect & rt) const;
};
struct RectIncludePoint : std::binary_function<Rect, Point, bool>
{
bool operator() (const Rect & r, const Point & p) const { return r & p; };
};
#endif

View File

@@ -1,575 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include "sdlnet.h"
#define BUFSIZE 16
QueueMessage::QueueMessage() : type(0), data(NULL), itd1(NULL), itd2(NULL), dtsz(BUFSIZE)
{
data = new char [dtsz + 1];
itd1 = data;
itd2 = itd1;
}
QueueMessage::QueueMessage(u16 id) : type(id), data(NULL), itd1(NULL), itd2(NULL), dtsz(BUFSIZE)
{
data = new char [dtsz + 1];
itd1 = data;
itd2 = itd1;
}
QueueMessage::QueueMessage(const QueueMessage & msg) : type(msg.type), data(NULL), itd1(NULL), itd2(NULL), dtsz(msg.dtsz)
{
data = new char [dtsz + 1];
std::memcpy(data, msg.data, dtsz);
itd1 = msg.itd1 > msg.data ? &data[msg.itd1 - msg.data] : data;
itd2 = msg.itd2 > msg.data ? &data[msg.itd2 - msg.data] : itd1;
}
QueueMessage & QueueMessage::operator= (const QueueMessage & msg)
{
type = msg.type;
dtsz = msg.dtsz;
data = new char [dtsz + 1];
std::memcpy(data, msg.data, dtsz);
itd1 = msg.itd1 > msg.data ? &data[msg.itd1 - msg.data] : data;
itd2 = msg.itd2 > msg.data ? &data[msg.itd2 - msg.data] : itd1;
return *this;
}
QueueMessage::~QueueMessage()
{
if(data) delete [] data;
}
void QueueMessage::Resize(size_t lack)
{
const size_t newsize = lack > dtsz ? dtsz + lack + 1 : 2 * dtsz + 1;
char* dat2 = new char [newsize];
std::memcpy(dat2, data, dtsz);
itd1 = &dat2[itd1 - data];
itd2 = &dat2[itd2 - data];
dtsz = newsize - 1;
delete [] data;
data = dat2;
}
size_t QueueMessage::Size(void) const
{
return itd2 - data;
}
u16 QueueMessage::GetID(void) const
{
return type;
}
void QueueMessage::SetID(u16 id)
{
type = id;
}
void QueueMessage::Reserve(size_t size)
{
delete [] data;
dtsz = size;
data = new char [dtsz + 1];
itd1 = data;
itd2 = itd1;
}
void QueueMessage::Reset(void)
{
type = 0;
if(BUFSIZE != dtsz)
{
delete [] data;
dtsz = BUFSIZE;
data = new char [dtsz + 1];
}
itd1 = data;
itd2 = itd1;
}
void QueueMessage::SoftReset(void)
{
itd1 = data;
}
void QueueMessage::Push(s8 byte8)
{
Push(static_cast<u8>(byte8));
}
void QueueMessage::Push(u8 byte8)
{
if(data + dtsz < itd2 + 1) Resize(1);
*itd2 = byte8;
++itd2;
}
void QueueMessage::Push(s16 byte16)
{
Push(static_cast<u16>(byte16));
}
void QueueMessage::Push(u16 byte16)
{
if(data + dtsz < itd2 + 2) Resize(2);
*itd2 = 0x00FF & (byte16 >> 8);
++itd2;
*itd2 = 0x00FF & byte16;
++itd2;
}
void QueueMessage::Push(s32 byte32)
{
Push(static_cast<u32>(byte32));
}
void QueueMessage::Push(u32 byte32)
{
if(data + dtsz < itd2 + 4) Resize(4);
*itd2 = 0x000000FF & (byte32 >> 24);
++itd2;
*itd2 = 0x000000FF & (byte32 >> 16);
++itd2;
*itd2 = 0x000000FF & (byte32 >> 8);
++itd2;
*itd2 = 0x000000FF & byte32;
++itd2;
}
void QueueMessage::Push(const std::string & str)
{
Push(str.c_str());
}
void QueueMessage::Push(const char* str)
{
const size_t len = std::strlen(str);
if(data + dtsz < itd2 + len + 1) Resize(len + 1);
while(*str) *itd2++ = *str++;
// end string
*itd2 = 0;
++itd2;
}
bool QueueMessage::Pop(s8 & byte8)
{
u8 tmp;
if(Pop(tmp))
{
byte8 = tmp;
return true;
}
return false;
}
bool QueueMessage::Pop(u8 & byte8)
{
if(itd1 + 1 > itd2) return false;
byte8 = *itd1;
++itd1;
return true;
}
bool QueueMessage::Pop(s16 & byte16)
{
u16 tmp;
if(Pop(tmp))
{
byte16 = tmp;
return true;
}
return false;
}
bool QueueMessage::Pop(u16 & byte16)
{
if(itd1 + 2 > itd2) return false;
byte16 = *itd1;
byte16 <<= 8;
++itd1;
byte16 |= (0x00FF & *itd1);
++itd1;
return true;
}
bool QueueMessage::Pop(s32 & byte32)
{
u32 tmp;
if(Pop(tmp))
{
byte32 = tmp;
return true;
}
return false;
}
bool QueueMessage::Pop(u32 & byte32)
{
if(itd1 + 4 > itd2) return false;
byte32 = *itd1;
byte32 <<= 8;
++itd1;
byte32 |= (0x000000FF & *itd1);
byte32 <<= 8;
++itd1;
byte32 |= (0x000000FF & *itd1);
byte32 <<= 8;
++itd1;
byte32 |= (0x000000FF & *itd1);
++itd1;
return true;
}
bool QueueMessage::Pop(std::string & str)
{
if(itd1 >= itd2) return false;
// find end string
char* end = itd1;
while(*end && end < itd2) ++end;
if(end == itd2) return false;
str = itd1;
itd1 = end + 1;
return true;
}
void QueueMessage::Dump(std::ostream & stream) const
{
stream << "Network::QueueMessage::Dump: type: 0x" << std::hex << type << ", size: " << std::dec << DtSz();
stream << ", data:";
const char* cur = itd1;
u8 cast;
while(cur < itd2){ cast = *cur; stream << " 0x" << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(cast); ++cur; }
stream << std::endl;
}
const char* QueueMessage::DtPt(void) const
{
return itd1;
}
size_t QueueMessage::DtSz(void) const
{
return itd2 > itd1 ? itd2 - itd1 : 0;
}
void QueueMessage::Save(const char* fn) const
{
std::ofstream fs(fn, std::ios::binary);
if(fs.is_open())
{
fs.write(DtPt(), DtSz());
fs.close();
}
}
void QueueMessage::Load(const char* fn)
{
std::ifstream fs(fn, std::ios::binary);
if(fs.is_open())
{
fs.seekg(0, std::ios_base::end);
dtsz = fs.tellg();
fs.seekg(0, std::ios_base::beg);
delete [] data;
data = new char [dtsz + 1];
fs.read(data, dtsz);
fs.close();
itd1 = data;
itd2 = itd1 + dtsz;
}
}
#ifdef WITH_NET
namespace Network
{
static u16 proto = 0xFF01;
}
void Network::SetProtocolVersion(u16 v)
{
proto = v;
}
bool Network::RecvMessage(const Network::Socket & csd, QueueMessage & msg, bool debug)
{
u16 head;
msg.type = 0;
if(csd.Recv(reinterpret_cast<char *>(&head), sizeof(head)))
{
SwapBE16(head);
// check id
if((0xFF00 & Network::proto) != (0xFF00 & head))
{
if(debug) std::cerr << "Network::QueueMessage::Recv: " << "unknown packet id: 0x" << std::hex << head << std::endl;
return false;
}
// check ver
if((0x00FF & Network::proto) > (0x00FF & head))
{
if(debug) std::cerr << "Network::QueueMessage::Recv: " << "obsolete protocol ver: 0x" << std::hex << (0x00FF & head) << std::endl;
return false;
}
u32 size;
if(csd.Recv(reinterpret_cast<char *>(&msg.type), sizeof(msg.type)) &&
csd.Recv(reinterpret_cast<char *>(&size), sizeof(size)))
{
msg.type = SDL_SwapBE16(msg.type);
size = SDL_SwapBE32(size);
if(size > msg.dtsz)
{
delete [] msg.data;
msg.data = new char [size + 1];
msg.dtsz = size;
}
msg.itd1 = msg.data;
msg.itd2 = msg.itd1 + size;
return size ? csd.Recv(msg.data, size) : true;
}
}
return false;
}
bool Network::SendMessage(const Network::Socket & csd, const QueueMessage & msg)
{
// raw data
if(0 == msg.type)
return msg.Size() ? csd.Send(reinterpret_cast<const char *>(msg.data), msg.Size()) : false;
u16 head = Network::proto;
u16 sign = msg.type;
u32 size = msg.Size();
SwapBE16(head);
SwapBE16(sign);
SwapBE32(size);
return csd.Send(reinterpret_cast<const char *>(&head), sizeof(head)) &&
csd.Send(reinterpret_cast<const char *>(&sign), sizeof(sign)) &&
csd.Send(reinterpret_cast<const char *>(&size), sizeof(size)) &&
(size ? csd.Send(msg.data, msg.Size()) : true);
}
Network::Socket::Socket() : sd(NULL), sdset(NULL)
{
}
Network::Socket::Socket(const TCPsocket csd) : sd(NULL), sdset(NULL)
{
Assign(csd);
}
Network::Socket::Socket(const Socket &) : sd(NULL), sdset(NULL)
{
}
Network::Socket & Network::Socket::operator= (const Socket &)
{
return *this;
}
Network::Socket::~Socket()
{
if(sd) Close();
}
void Network::Socket::Assign(const TCPsocket csd)
{
if(sd) Close();
if(csd)
{
sd = csd;
sdset = SDLNet_AllocSocketSet(1);
if(sdset) SDLNet_TCP_AddSocket(sdset, sd);
}
}
u32 Network::Socket::Host(void) const
{
IPaddress* remoteIP = sd ? SDLNet_TCP_GetPeerAddress(sd) : NULL;
if(remoteIP) return SDLNet_Read32(&remoteIP->host);
std::cerr << "Network::Socket::Host: " << GetError() << std::endl;
return 0;
}
u16 Network::Socket::Port(void) const
{
IPaddress* remoteIP = sd ? SDLNet_TCP_GetPeerAddress(sd) : NULL;
if(remoteIP) return SDLNet_Read16(&remoteIP->port);
std::cerr << "Network::Socket::Host: " << GetError() << std::endl;
return 0;
}
bool Network::Socket::Ready(void) const
{
return 0 < SDLNet_CheckSockets(sdset, 1) && 0 < SDLNet_SocketReady(sd);
}
bool Network::Socket::Recv(char *buf, size_t len) const
{
if(sd && buf && len)
{
int rcv = 0;
while((rcv = SDLNet_TCP_Recv(sd, buf, len)) > 0 && rcv < static_cast<int>(len))
{
buf += rcv;
len -= rcv;
}
if(rcv == static_cast<int>(len)) return true;
std::cerr << "Network::Socket::Recv: " << "size: " << std::dec << len << ", receive: " << rcv << ", error: " << GetError() << std::endl;
}
return false;
}
bool Network::Socket::Send(const char* buf, size_t len) const
{
return sd && static_cast<int>(len) == SDLNet_TCP_Send(sd, const_cast<void*>(reinterpret_cast<const void*>(buf)), len);
}
bool Network::Socket::Open(IPaddress & ip)
{
Assign(SDLNet_TCP_Open(&ip));
if(! sd)
std::cerr << "Network::Socket::Open: " << Network::GetError() << std::endl;
return sd;
}
bool Network::Socket::IsValid(void) const
{
return sd;
}
void Network::Socket::Close(void)
{
if(sd)
{
if(sdset)
{
SDLNet_TCP_DelSocket(sdset, sd);
SDLNet_FreeSocketSet(sdset);
sdset = NULL;
}
SDLNet_TCP_Close(sd);
sd = NULL;
}
}
Network::Server::Server()
{
}
TCPsocket Network::Server::Accept(void)
{
return SDLNet_TCP_Accept(sd);
}
bool Network::Init(void)
{
if(SDLNet_Init() < 0)
{
std::cerr << "Network::Init: " << GetError() << std::endl;
return false;
}
return true;
}
void Network::Quit(void)
{
SDLNet_Quit();
}
bool Network::ResolveHost(IPaddress & ip, const char* host, u16 port)
{
if(SDLNet_ResolveHost(&ip, host, port) < 0)
{
std::cerr << "Network::ResolveHost: " << GetError() << std::endl;
return false;
}
return true;
}
const char* Network::GetError(void)
{
return SDLNet_GetError();
}
#endif

View File

@@ -1,141 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef SDLNET_H
#define SDLNET_H
#include <string>
#include "types.h"
#include <iostream>
#ifdef WITH_NET
#include "SDL_net.h"
class QueueMessage;
namespace Network
{
bool Init(void);
void Quit(void);
bool ResolveHost(IPaddress &, const char*, u16);
const char* GetError(void);
void SetProtocolVersion(u16);
class Socket
{
public:
Socket();
Socket(const TCPsocket);
~Socket();
void Assign(const TCPsocket);
bool Ready(void) const;
bool Recv(char *, size_t) const;
bool Send(const char*, size_t) const;
u32 Host(void) const;
u16 Port(void) const;
bool Open(IPaddress &);
bool IsValid(void) const;
void Close(void);
protected:
Socket(const Socket &);
Socket & operator= (const Socket &);
TCPsocket sd;
SDLNet_SocketSet sdset;
};
class Server : public Socket
{
public:
Server();
TCPsocket Accept(void);
};
bool RecvMessage(const Network::Socket &, QueueMessage &, bool = false);
bool SendMessage(const Network::Socket &, const QueueMessage &);
}
#endif
class QueueMessage
{
public:
QueueMessage();
QueueMessage(u16);
QueueMessage(const QueueMessage &);
~QueueMessage();
QueueMessage & operator= (const QueueMessage &);
u16 GetID(void) const;
void SetID(u16);
void Push(u8);
void Push(s8);
void Push(u16);
void Push(s16);
void Push(u32);
void Push(s32);
void Push(const std::string &);
void Push(const char*);
bool Pop(u8 &);
bool Pop(s8 &);
bool Pop(u16 &);
bool Pop(s16 &);
bool Pop(u32 &);
bool Pop(s32 &);
bool Pop(std::string &);
void SoftReset(void);
void Reset(void);
void Reserve(size_t);
void Dump(std::ostream & = std::cerr) const;
const char* DtPt(void) const;
size_t DtSz(void) const;
void Save(const char*) const;
void Load(const char*);
protected:
#ifdef WITH_NET
friend bool Network::RecvMessage(const Network::Socket &, QueueMessage &, bool);
friend bool Network::SendMessage(const Network::Socket &, const QueueMessage &);
#endif
void Resize(size_t);
size_t Size(void) const;
u16 type;
char* data;
char* itd1;
char* itd2;
size_t dtsz;
};
#endif

View File

@@ -1,124 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "rect.h"
#include "surface.h"
#include "display.h"
#include "spritecursor.h"
SpriteCursor::SpriteCursor() : Background(), sprite(NULL), visible(false)
{
}
SpriteCursor::SpriteCursor(const Surface &cursor, const Point & pt) : Background(pt, cursor.w(), cursor.h()), sprite(&cursor), visible(false)
{
}
SpriteCursor::SpriteCursor(const Surface &cursor, s16 x, s16 y) : Background(x, y, cursor.w(), cursor.h()), sprite(&cursor), visible(false)
{
}
u16 SpriteCursor::w(void) const
{
return sprite ? sprite->w() : 0;
}
u16 SpriteCursor::h(void) const
{
return sprite ? sprite->h() : 0;
}
void SpriteCursor::SetSprite(const Surface & sf)
{
if(visible) Restore();
Save(Background::x, Background::y, sf.w(), sf.h());
sprite = &sf;
}
const Surface* SpriteCursor::Sprite(void)
{
return sprite && sprite->isValid() ? sprite : NULL;
}
void SpriteCursor::Move(const Point &pt)
{
Move(pt.x, pt.y);
}
void SpriteCursor::Move(s16 ax, s16 ay)
{
if(Background::x == ax && Background::y == ay)
{
if(!visible) Show();
}
else
{
if(visible) Hide();
Show(ax, ay);
}
}
void SpriteCursor::Hide(void)
{
if(!visible) return;
Restore();
visible = false;
}
void SpriteCursor::Redraw(void)
{
if(visible)
{
Hide();
Show();
}
}
void SpriteCursor::Show(void)
{
Show(GetPos());
}
void SpriteCursor::Show(const Point &pt)
{
Show(pt.x, pt.y);
}
void SpriteCursor::Show(s16 ax, s16 ay)
{
if(visible) return;
Save(ax, ay);
if(sprite) Display::Get().Blit(*sprite, ax, ay);
visible = true;
}
bool SpriteCursor::isVisible(void) const
{
return visible;
}

View File

@@ -1,63 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2SPRITECURSOR_H
#define H2SPRITECURSOR_H
#include "background.h"
#include "types.h"
class Point;
class Surface;
class SpriteCursor : public Background
{
public:
/* sprite cursor */
SpriteCursor();
SpriteCursor(const Surface &cursor, const Point & pt = Point());
SpriteCursor(const Surface &cursor, s16 x, s16 y);
u16 w(void) const;
u16 h(void) const;
void SetSprite(const Surface & sf);
const Surface* Sprite(void);
void Move(s16 ax, s16 ay);
void Move(const Point &pt);
void Hide(void);
void Show(s16 ax, s16 ay);
void Show(const Point &pt);
void Show(void);
void Redraw(void);
bool isVisible(void) const;
private:
const Surface* sprite;
bool visible;
};
#endif

View File

@@ -1,940 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <algorithm>
#include <iostream>
#include <cstring>
#include "surface.h"
#include "palette.h"
#include "error.h"
#include "localevent.h"
#include "display.h"
#ifdef WITH_TTF
#include "SDL_ttf.h"
#endif
#ifdef WITH_IMAGE
#include "SDL_image.h"
#include "IMG_savepng.h"
#endif
static u8 default_depth = 16;
static SDL_Color* pal_colors = NULL;
#define pal_ncolors 255
void SDLFreeSurface(SDL_Surface *sf)
{
if(sf)
{
// clear static palette
if(sf->format && 8 == sf->format->BitsPerPixel && sf->format->palette && pal_colors == sf->format->palette->colors)
{
sf->format->palette->colors = NULL;
sf->format->palette->ncolors = 0;
}
SDL_FreeSurface(sf);
}
}
Surface::Surface() : surface(NULL)
{
}
Surface::Surface(const void* pixels, unsigned int width, unsigned int height, unsigned char bytes_per_pixel, bool alpha) : surface(NULL)
{
Set(pixels, width, height, bytes_per_pixel, alpha);
}
Surface::Surface(u16 sw, u16 sh, u8 depth, u32 fl) : surface(NULL)
{
Set(sw, sh, depth, fl);
}
Surface::Surface(u16 sw, u16 sh, bool alpha) : surface(NULL)
{
Set(sw, sh, alpha);
}
Surface::Surface(const Surface & bs) : surface(NULL)
{
Set(bs);
}
Surface::Surface(SDL_Surface* sf) : surface(NULL)
{
Set(sf);
}
Surface::~Surface()
{
if(! isDisplay())
FreeSurface(*this);
}
/* operator = */
Surface & Surface::operator= (const Surface & bs)
{
Set(bs);
return *this;
}
void Surface::SetDefaultDepth(u8 depth)
{
switch(depth)
{
case 8:
case 16:
case 24:
case 32:
default_depth = depth;
break;
default:
break;
}
}
u8 Surface::GetDefaultDepth(void)
{
return default_depth;
}
void Surface::Set(SDL_Surface* sf)
{
FreeSurface(*this);
surface = sf ? sf : NULL;
LoadPalette();
}
void Surface::Set(const Surface & bs)
{
FreeSurface(*this);
if(bs.surface)
{
surface = SDL_ConvertSurface(bs.surface, bs.surface->format, bs.surface->flags);
if(!surface) std::cerr << "Surface: copy constructor, error: " << SDL_GetError() << std::endl;
}
}
void Surface::Set(u16 sw, u16 sh, bool alpha)
{
FreeSurface(*this);
CreateSurface(sw, sh, default_depth, 8 < default_depth && alpha ? SDL_SRCALPHA|SDL_SWSURFACE : SDL_SWSURFACE);
LoadPalette();
}
void Surface::Set(u16 sw, u16 sh, u8 depth, u32 fl)
{
FreeSurface(*this);
CreateSurface(sw, sh, depth, fl);
LoadPalette();
}
void Surface::Set(const void* pixels, unsigned int width, unsigned int height, unsigned char bytes_per_pixel, bool alpha)
{
FreeSurface(*this);
switch(bytes_per_pixel)
{
case 1:
Set(width, height, 8, SDL_SWSURFACE);
Lock();
memcpy(surface->pixels, pixels, width * height);
Unlock();
break;
default:
{
u32 rmask = 0;
u32 gmask = 0;
u32 bmask = 0;
u32 amask = 0;
switch(bytes_per_pixel)
{
case 4:
rmask = RMASK32;
gmask = GMASK32;
bmask = BMASK32;
amask = alpha ? AMASK32 : 0;
break;
case 3:
rmask = alpha ? RMASK24 : RMASK32;
gmask = alpha ? GMASK24 : GMASK32;
bmask = alpha ? BMASK24 : BMASK32;
amask = alpha ? AMASK24 : 0;
break;
case 2:
rmask = RMASK16;
gmask = GMASK16;
bmask = BMASK16;
amask = alpha ? AMASK16 : 0;
break;
default: break;
}
surface = SDL_CreateRGBSurfaceFrom(const_cast<void *>(pixels), width, height, 8 * bytes_per_pixel, width * bytes_per_pixel,
rmask, gmask, bmask, amask);
}
break;
}
}
bool Surface::isDisplay(void) const
{
return NULL != surface && Display::Get().surface == surface;
}
bool Surface::Load(const char* fn)
{
FreeSurface(*this);
#ifdef WITH_IMAGE
if(fn) surface = IMG_Load(fn);
#else
if(fn) surface = SDL_LoadBMP(fn);
#endif
return surface;
}
bool Surface::Load(const std::string & str)
{
return Load(str.c_str());
}
bool Surface::Save(const char *fn) const
{
#ifdef WITH_IMAGE
return !surface || !fn || IMG_SavePNG(fn, surface, -1) ? false : true;
#else
return !surface || !fn || SDL_SaveBMP(surface, fn) ? false : true;
#endif
}
bool Surface::Save(const std::string & str) const
{
return Save(str.c_str());
}
u16 Surface::w(void) const
{
return surface ? surface->w : 0;
}
u16 Surface::h(void) const
{
return surface ? surface->h : 0;
}
u8 Surface::depth(void) const
{
return surface ? surface->format->BitsPerPixel : 0;
}
bool Surface::isAlpha(void) const
{
return SDL_SRCALPHA & surface->flags;
}
u8 Surface::GetAlpha(void) const
{
#if SDL_VERSION_ATLEAST(1, 3, 0)
u8 alpha = 0;
if(surface) SDL_GetSurfaceAlphaMod(surface, &alpha);
return alpha;
#else
return surface->format->alpha;
#endif
}
u32 Surface::MapRGB(u8 r, u8 g, u8 b, u8 a) const
{
return (SDL_SRCALPHA & surface->flags) ? SDL_MapRGBA(surface->format, r, g, b, a) : SDL_MapRGB(surface->format, r, g, b);
}
void Surface::GetRGB(u32 pixel, u8 *r, u8 *g, u8 *b, u8 *a) const
{
return (SDL_SRCALPHA & surface->flags && a) ? SDL_GetRGBA(pixel, surface->format, r, g, b, a) : SDL_GetRGB(pixel, surface->format, r, g, b);
}
/* create new surface */
void Surface::CreateSurface(u16 sw, u16 sh, u8 dp, u32 fl)
{
switch(dp)
{
case 32:
surface = SDL_CreateRGBSurface(fl, sw, sh, dp, RMASK32, GMASK32, BMASK32, (SDL_SRCALPHA & fl ? AMASK32 : 0));
break;
case 24:
surface = SDL_CreateRGBSurface(fl, sw, sh, dp, RMASK24, GMASK24, BMASK24, (SDL_SRCALPHA & fl ? AMASK24 : 0));
break;
case 16:
surface = SDL_CreateRGBSurface(fl, sw, sh, dp, RMASK16, GMASK16, BMASK16, (SDL_SRCALPHA & fl ? AMASK16 : 0));
break;
default:
surface = SDL_CreateRGBSurface(fl, sw, sh, dp, 0, 0, 0, 0);
break;
}
if(!surface)
{
std::cerr << "w: " << sw << ", h: " << sh << std::endl;
Error::Except("Surface::CreateSurface: empty surface, error:", SDL_GetError());
}
}
void Surface::LoadPalette(void)
{
// only 8bit color
// load static palette (economize 1kb for each surface)
if(surface && 8 == surface->format->BitsPerPixel)
{
if(!pal_colors)
{
pal_colors = Palette::Get().SDLPalette()->colors;
}
if(surface->format->palette)
{
if(surface->format->palette->colors && pal_colors != surface->format->palette->colors) SDL_free(surface->format->palette->colors);
surface->format->palette->colors = pal_colors;
surface->format->palette->ncolors = pal_ncolors;
}
}
}
/* format surface */
void Surface::SetDisplayFormat(void)
{
SDL_Surface *osurface = surface;
surface = SDL_DisplayFormatAlpha(osurface);
if(osurface) SDLFreeSurface(osurface);
}
u32 Surface::GetColor(u16 index) const
{
if(! surface) return 0;
return 8 == surface->format->BitsPerPixel ? index : Palette::Get().GetColor(index);
}
u32 Surface::GetColorKey(void) const
{
if(! surface) return 0;
return SDL_MapRGBA(surface->format, 0xFF, 0x00, 0xFF, 0);
}
/* set color key */
void Surface::SetColorKey(void)
{
if(surface)
{
const u32 clkey = GetColorKey();
Fill(clkey);
SetColorKey(clkey);
}
}
void Surface::SetColorKey(u8 r, u8 g, u8 b)
{
#ifdef _WIN32_WCE
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, MapRGB(r, g, b));
#else
SDL_SetColorKey(surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, MapRGB(r, g, b));
#endif
}
void Surface::SetColorKey(u32 color)
{
#ifdef _WIN32_WCE
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, color);
#else
SDL_SetColorKey(surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, color);
#endif
}
/* draw u32 pixel */
void Surface::SetPixel4(u16 x, u16 y, u32 color)
{
if(x > surface->w || y > surface->h) return;
u32 *bufp = static_cast<u32 *>(surface->pixels) + y * surface->pitch / 4 + x;
*bufp = color;
}
/* draw u24 pixel */
void Surface::SetPixel3(u16 x, u16 y, u32 color)
{
if(x > surface->w || y > surface->h) return;
u8 *bufp = static_cast<u8 *>(surface->pixels) + y * surface->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
}
else
{
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
/* draw u16 pixel */
void Surface::SetPixel2(u16 x, u16 y, u32 color)
{
if(x > surface->w || y > surface->h) return;
u16 *bufp = static_cast<u16 *>(surface->pixels) + y * surface->pitch / 2 + x;
*bufp = static_cast<u16>(color);
}
/* draw u8 pixel */
void Surface::SetPixel1(u16 x, u16 y, u32 color)
{
if(x > surface->w || y > surface->h) return;
u8 *bufp = static_cast<u8 *>(surface->pixels) + y * surface->pitch + x;
*bufp = static_cast<u8>(color);
}
/* draw pixel */
void Surface::SetPixel(u16 x, u16 y, u32 color)
{
switch(surface->format->BytesPerPixel)
{
case 1: SetPixel1(x, y, color); break;
case 2: SetPixel2(x, y, color); break;
case 3: SetPixel3(x, y, color); break;
case 4: SetPixel4(x, y, color); break;
default: break;
}
if(isDisplay()) Display::Get().AddUpdateRect(x, y, 1, 1);
}
u32 Surface::GetPixel4(u16 x, u16 y) const
{
if(x > surface->w || y > surface->h) return 0;
u32 *bufp = static_cast<u32 *>(surface->pixels) + y * surface->pitch / 4 + x;
return *bufp;
}
u32 Surface::GetPixel3(u16 x, u16 y) const
{
if(x > surface->w || y > surface->h) return 0;
u8 *bufp = static_cast<u8 *>(surface->pixels) + y * surface->pitch + x * 3;
u32 color = 0;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
color |= bufp[2];
color <<= 8;
color |= bufp[1];
color <<= 8;
color |= bufp[0];
}
else
{
color |= bufp[0];
color <<= 8;
color |= bufp[1];
color <<= 8;
color |= bufp[2];
}
return color;
}
u32 Surface::GetPixel2(u16 x, u16 y) const
{
if(x > surface->w || y > surface->h) return 0;
u16 *bufp = static_cast<u16 *>(surface->pixels) + y * surface->pitch / 2 + x;
return static_cast<u32>(*bufp);
}
u32 Surface::GetPixel1(u16 x, u16 y) const
{
if(x > surface->w || y > surface->h) return 0;
u8 *bufp = static_cast<u8 *>(surface->pixels) + y * surface->pitch + x;
return static_cast<u32>(*bufp);
}
u32 Surface::GetPixel(u16 x, u16 y) const
{
switch(surface->format->BytesPerPixel)
{
case 1: return GetPixel1(x, y);
case 2: return GetPixel2(x, y);
case 3: return GetPixel3(x, y);
case 4: return GetPixel4(x, y);
default: break;
}
return 0;
}
/* fill colors surface */
void Surface::Fill(u32 color)
{
SDL_Rect dstrect = {0, 0, surface->w, surface->h};
SDL_FillRect(surface, &dstrect, color);
if(isDisplay()) Display::Get().AddUpdateRect(0, 0, surface->w, surface->h);
}
/* rect fill colors surface */
void Surface::FillRect(u32 color, const Rect & rect)
{
SDL_Rect dstrect = {rect.x, rect.y, rect.w, rect.h};
SDL_FillRect(surface, &dstrect, color);
if(isDisplay()) Display::Get().AddUpdateRect(rect.x, rect.y, rect.w, rect.h);
}
/* blit */
void Surface::Blit(const Surface &src)
{
SDL_BlitSurface(src.surface, NULL, surface, NULL);
if(isDisplay()) Display::Get().AddUpdateRect(0, 0, src.w(), src.h());
}
/* blit */
void Surface::Blit(const Surface &src, s16 dst_ox, s16 dst_oy)
{
SDL_Rect dstrect = {dst_ox, dst_oy, src.surface->w, src.surface->h};
SDL_BlitSurface(src.surface, NULL, surface, &dstrect);
if(isDisplay()) Display::Get().AddUpdateRect(dst_ox, dst_oy, src.surface->w, src.surface->h);
}
/* blit */
void Surface::Blit(const Surface &src, const Rect &src_rt, s16 dst_ox, s16 dst_oy)
{
SDL_Rect dstrect = {dst_ox, dst_oy, src_rt.w, src_rt.h};
SDL_Rect srcrect = {src_rt.x, src_rt.y, src_rt.w, src_rt.h};
SDL_BlitSurface(src.surface, &srcrect, surface, &dstrect);
if(isDisplay()) Display::Get().AddUpdateRect(dst_ox, dst_oy, src_rt.w, src_rt.h);
}
void Surface::Blit(const Surface &src, const Point &dst_pt)
{
Blit(src, dst_pt.x, dst_pt.y);
}
void Surface::Blit(const Surface &src, const Rect &src_rt, const Point &dst_pt)
{
Blit(src, src_rt, dst_pt.x, dst_pt.y);
}
void Surface::SetAlpha(u8 level)
{
#if SDL_VERSION_ATLEAST(1, 3, 0)
if(surface)
SDL_SetSurfaceAlphaMod(surface, level);
#else
if(surface)
SDL_SetAlpha(surface, SDL_SRCALPHA, level);
#endif
}
void Surface::ResetAlpha(void)
{
if(!surface) return;
SDL_SetAlpha(surface, 0, 255);
}
void Surface::Lock(void) const
{
if(SDL_MUSTLOCK(surface)) SDL_LockSurface(surface);
}
void Surface::Unlock(void) const
{
if(SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);
}
void Surface::FreeSurface(Surface & sf)
{
if(sf.surface)
{
SDLFreeSurface(sf.surface);
sf.surface = NULL;
}
}
const SDL_PixelFormat *Surface::GetPixelFormat(void) const
{
return surface ? surface->format : NULL;
}
void Surface::ChangeColorIndex(u32 fc, u32 tc)
{
if(!surface) return;
if(8 != depth()) return ChangeColor(GetColor(fc), GetColor(tc));
Lock();
if(fc != tc)
for(u16 y = 0; y < surface->h; ++y)
for(u16 x = 0; x < surface->w; ++x)
if(fc == GetPixel(x, y)) SetPixel(x, y, tc);
Unlock();
}
void Surface::ChangeColor(u32 fc, u32 tc)
{
if(!surface) return;
Lock();
if(fc != tc)
for(u16 y = 0; y < surface->h; ++y)
for(u16 x = 0; x < surface->w; ++x)
if(fc == GetPixel(x, y)) SetPixel(x, y, tc);
Unlock();
}
void Surface::GrayScale(void)
{
if(!surface) return;
u8 a, r, g, b, z;
const u32 colkey = GetColorKey();
u32 color = 0;
Lock();
for(u16 y = 0; y < surface->h; ++y)
for(u16 x = 0; x < surface->w; ++x)
{
color = GetPixel(x, y);
if(color == colkey) continue;
GetRGB(color, &r, &g, &b, &a);
z = static_cast<u8>(0.299 * r + 0.587 * g + 0.114 * b);
r = z;
g = z;
b = z;
SetPixel(x, y, MapRGB(r, g, b, a));
}
Unlock();
}
void Surface::Sepia(void)
{
if(!surface) return;
Lock();
for(u16 x = 0; x < surface->w; x++)
for(u16 y = 0; y < surface->h; y++)
{
u32 pixel = GetPixel(x, y);
u8 r, g, b;
GetRGB(pixel, &r, &g, &b);
//Numbers derived from http://blogs.techrepublic.com.com/howdoi/?p=120
#define CLAMP255(val) static_cast<u8>(std::min<u16>((val), 255))
u8 outR = CLAMP255(static_cast<u16>(r * 0.693f + g * 0.769f + b * 0.189f));
u8 outG = CLAMP255(static_cast<u16>(r * 0.449f + g * 0.686f + b * 0.168f));
u8 outB = CLAMP255(static_cast<u16>(r * 0.272f + g * 0.534f + b * 0.131f));
pixel = MapRGB(outR, outG, outB);
SetPixel(x, y, pixel);
#undef CLAMP255
}
Unlock();
}
void Surface::DrawLine(const Point & p1, const Point & p2, u32 c)
{
DrawLine(p1.x, p1.y, p2.x, p2.y, c);
}
void Surface::DrawLine(u16 x1, u16 y1, u16 x2, u16 y2, u32 c)
{
const u16 dx = std::abs(x2 - x1);
const u16 dy = std::abs(y2 - y1);
Lock();
if(dx > dy)
{
s16 ns = std::div(dx, 2).quot;
for(u16 i = 0; i <= dx; ++i)
{
SetPixel(x1, y1, c);
x1 < x2 ? ++x1 : --x1;
ns -= dy;
if(ns < 0)
{
y1 < y2 ? ++y1 : --y1;
ns += dx;
}
}
}
else
{
s16 ns = std::div(dy, 2).quot;
for(u16 i = 0; i <= dy; ++i)
{
SetPixel(x1, y1, c);
y1 < y2 ? ++y1 : --y1;
ns -= dx;
if(ns < 0)
{
x1 < x2 ? ++x1 : --x1;
ns += dy;
}
}
}
Unlock();
}
void Surface::MakeStencil(Surface & dst, const Surface & src, u32 col)
{
if(!src.surface) return;
dst.Set(src.surface->w, src.surface->h);
dst.SetColorKey();
const u32 clkey = src.GetColorKey();
u8 r, g, b, a;
src.Lock();
dst.Lock();
for(u16 y = 0; y < src.surface->h; ++y)
for(u16 x = 0; x < src.surface->w; ++x)
{
u32 pixel = src.GetPixel(x, y);
if(clkey != pixel)
{
if(src.isAlpha())
{
src.GetRGB(pixel, &r, &g, &b, &a);
// skip shadow
if(a < 200) continue;
}
dst.SetPixel(x, y, col);
}
}
dst.Unlock();
src.Unlock();
}
void Surface::MakeContour(Surface & dst, const Surface & src, u32 col)
{
if(!src.surface) return;
dst.Set(src.surface->w + 2, src.surface->h + 2);
dst.SetColorKey();
Surface trf;
u32 fake = src.MapRGB(0x00, 0xFF, 0xFF);
MakeStencil(trf, src, fake);
const u32 clkey = trf.GetColorKey();
trf.Lock();
dst.Lock();
for(u16 y = 0; y < trf.h(); ++y)
for(u16 x = 0; x < trf.w(); ++x)
{
if(fake == trf.GetPixel(x, y))
{
if(0 == x) dst.SetPixel(x, y, col);
else if(trf.w() - 1 == x) dst.SetPixel(x + 1, y, col);
else if(0 == y) dst.SetPixel(x, y, col);
else if(trf.h() - 1 == y) dst.SetPixel(x, y + 1, col);
else {
if(0 < x && clkey == trf.GetPixel(x - 1, y)) dst.SetPixel(x - 1, y, col);
if(trf.w() - 1 > x && clkey == trf.GetPixel(x + 1, y)) dst.SetPixel(x + 1, y, col);
if(0 < y && clkey == trf.GetPixel(x, y - 1)) dst.SetPixel(x, y - 1, col);
if(trf.h() - 1 > y && clkey == trf.GetPixel(x, y + 1)) dst.SetPixel(x, y + 1, col);
}
}
}
trf.Unlock();
dst.Unlock();
}
void Surface::TILReflect(Surface & sf_dst, const Surface & sf_src, const u8 shape)
{
// valid sf_src
if(!sf_src.surface || sf_src.w() != sf_src.h())
{
std::cerr << "Surface::TILReflect: " << "incorrect size" << std::endl;
return;
}
if(sf_src.depth() != 8)
{
std::cerr << "Surface::TILReflect: " << "incorrect depth, use only 8 bpp" << std::endl;
return;
}
const u8 tile_width = sf_src.w();
const u8 tile_height = sf_src.h();
// valid sf_dst
if(!sf_dst.surface || sf_dst.w() != tile_width || sf_dst.h() != tile_height)
{
sf_dst = Surface(tile_width, tile_height, 8, SWSURFACE);
}
const char* src = static_cast<const char*>(sf_src.surface->pixels);
char* dst = static_cast<char*>(sf_dst.surface->pixels);
s16 x, y;
char * dst2 = NULL;
sf_dst.Lock();
// draw tiles
switch(shape % 4)
{
// normal
case 0:
std::memcpy(dst, src, tile_width * tile_height);
break;
// vertical reflect
case 1:
{
dst2 = dst + tile_width * (tile_height - 1);
for(int i = 0; i < tile_height; i++)
{
memcpy(dst2, src, tile_width);
src += tile_width;
dst2 -= tile_width;
}
}
break;
// horizontal reflect
case 2:
for(y = 0; y < tile_height; ++y)
for(x = tile_width - 1; x >= 0; --x)
{
dst2 = dst + y * tile_width + x;
*dst2 = *src;
++src;
}
break;
// any variant
case 3:
for(y = tile_height - 1; y >= 0; --y)
for( x = tile_width - 1; x >= 0; --x)
{
dst2 = dst + y * tile_width + x;
*dst2 = *src;
++src;
}
break;
}
sf_dst.Unlock();
}
u32 Surface::GetSize(void) const
{
u32 res = 0;
if(surface)
{
res = sizeof(SDL_Surface) + sizeof(SDL_PixelFormat) + surface->pitch * surface->h;
if(surface->format->palette) res += sizeof(SDL_Palette) + surface->format->palette->ncolors * sizeof(SDL_Color);
}
return res;
}
u32 AVERAGE(SDL_PixelFormat* fm, u32 c1, u32 c2)
{
if(c1 == c2) return c1;
if(c1 == SDL_MapRGBA(fm, 0xFF, 0x00, 0xFF, 0)) c1 = 0;
if(c2 == SDL_MapRGBA(fm, 0xFF, 0x00, 0xFF, 0)) c2 = 0;
#define avr(a, b) ((a + b) >> 1)
u8 r1, g1, b1, a1;
SDL_GetRGBA(c1, fm, &r1, &g1, &b1, &a1);
u8 r2, g2, b2, a2;
SDL_GetRGBA(c2, fm, &r2, &g2, &b2, &a2);
return SDL_MapRGBA(fm, avr(r1, r2), avr(g1, g2), avr(b1, b2), avr(a1, a2));
}
/* scale surface */
void Surface::ScaleMinifyByTwo(Surface & sf_dst, const Surface & sf_src, bool event)
{
if(!sf_src.isValid()) { std::cerr << "Surface::ScaleMinifyByTwo: " << "invalid surface" << std::endl; return; };
u16 x, y, x2, y2;
u8 mul = 2;
u16 w = sf_src.w() / mul;
u16 h = sf_src.h() / mul;
if(2 > w || 2 > h){ std::cerr << "Surface::ScaleMinifyByTwo: " << "small size" << std::endl; return; };
sf_dst.Set(w, h, sf_src.depth(), SWSURFACE);
sf_dst.SetColorKey();
sf_dst.Lock();
sf_src.Lock();
for(y = 0; y < h; y++)
{
y2 = mul * y;
for(x = 0; x < w; x++)
{
x2 = mul * x;
const u32 & p = AVERAGE(sf_src.surface->format, sf_src.GetPixel(x2, y2), sf_src.GetPixel(x2 + 1, y2));
const u32 & q = AVERAGE(sf_src.surface->format, sf_src.GetPixel(x2, y2 + 1), sf_src.GetPixel(x2 + 1, y2 + 1));
sf_dst.SetPixel(x, y, AVERAGE(sf_src.surface->format, p, q));
if(event) LocalEvent::Get().HandleEvents(false);
}
}
sf_src.Unlock();
sf_dst.Unlock();
}
void Surface::Swap(Surface & sf1, Surface & sf2)
{
std::swap(sf1.surface, sf2.surface);
}

View File

@@ -1,151 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2SURFACE_H
#define H2SURFACE_H
#include <string>
#include "rect.h"
#include "types.h"
#define SWSURFACE SDL_SWSURFACE
class Palette;
class Point;
class Rect;
struct SDL_Surface;
struct SDL_PixelFormat;
#ifdef WITH_TTF
namespace SDL { class Font; }
#endif
class Surface
{
public:
Surface();
Surface(const void* pixels, unsigned int width, unsigned int height, unsigned char bytes_per_pixel, bool alpha);
Surface(u16 sw, u16 sh, u8 depth, u32 fl);
Surface(u16 sw, u16 sh, bool alpha = false);
Surface(const Surface & bs);
Surface(SDL_Surface * sf);
~Surface();
Surface & operator= (const Surface & bs);
void Set(const Surface &);
void Set(SDL_Surface * sf);
void Set(u16 sw, u16 sh, bool alpha = false);
void Set(u16 sw, u16 sh, u8 depth, u32 fl);
void Set(const void* pixels, unsigned int width, unsigned int height, unsigned char bytes_per_pixel, bool alpha);
bool Load(const char*);
bool Load(const std::string &);
bool Save(const char *) const;
bool Save(const std::string &) const;
u16 w(void) const;
u16 h(void) const;
u8 depth(void) const;
bool isValid(void) const{ return surface ? true : false; };
bool isDisplay(void) const;
bool isAlpha(void) const;
u8 GetAlpha(void) const;
u32 MapRGB(u8 r, u8 g, u8 b, u8 a = 0) const;
void GetRGB(u32 pixel, u8 *r, u8 *g, u8 *b, u8 *a = NULL) const;
void Blit(const Surface &src);
void Blit(const Surface &src, s16 dst_ox, s16 dst_oy);
void Blit(const Surface &src, const Point &dst_pt);
void Blit(const Surface &src, const Rect &src_rt, s16 dst_ox, s16 dst_oy);
void Blit(const Surface &src, const Rect &src_rt, const Point &dst_pt);
const SDL_Surface *GetSurface(void) const{ return surface; };
void Fill(u32 color);
void Fill(u8 r, u8 g, u8 b){ Fill(MapRGB(r, g, b)); };
void FillRect(u32 color, const Rect & src);
void FillRect(u8 r, u8 g, u8 b, const Rect & src){ FillRect(MapRGB(r, g, b), src); };
void SetDisplayFormat(void);
void SetColorKey(void);
void SetColorKey(u8 r, u8 g, u8 b);
void SetColorKey(u32 color);
void SetAlpha(u8 level);
void ResetAlpha(void);
void SetPixel(u16 x, u16 y, u32 color);
void LoadPalette(void);
u32 GetColorKey(void) const;
u32 GetColor(u16) const;
u32 GetPixel(u16 x, u16 y) const;
void DrawLine(const Point &, const Point &, u32);
void DrawLine(u16, u16, u16, u16, u32);
void ChangeColor(u32, u32);
void ChangeColorIndex(u32, u32);
void GrayScale(void);
void Sepia(void);
void Lock(void) const;
void Unlock(void) const;
u32 GetSize(void) const;
static void TILReflect(Surface & sf_dst, const Surface & sf_src, const u8 shape);
static void MakeStencil(Surface &, const Surface &, u32);
static void MakeContour(Surface &, const Surface &, u32);
static void ScaleMinifyByTwo(Surface & sf_dst, const Surface & sf_src, bool event = false);
static void SetDefaultDepth(u8);
static u8 GetDefaultDepth(void);
static void FreeSurface(Surface &);
static void Swap(Surface &, Surface &);
protected:
void SetPixel4(u16 x, u16 y, u32 color);
void SetPixel3(u16 x, u16 y, u32 color);
void SetPixel2(u16 x, u16 y, u32 color);
void SetPixel1(u16 x, u16 y, u32 color);
u32 GetPixel4(u16 x, u16 y) const;
u32 GetPixel3(u16 x, u16 y) const;
u32 GetPixel2(u16 x, u16 y) const;
u32 GetPixel1(u16 x, u16 y) const;
#ifdef WITH_TTF
friend class SDL::Font;
#endif
friend class Palette;
void CreateSurface(const Rect &sz, u8 dp, u32 fl){ CreateSurface(sz.w, sz.h, dp, fl); };
void CreateSurface(u16 sw, u16 sh, u8 dp, u32 fl);
const SDL_PixelFormat *GetPixelFormat(void) const;
SDL_Surface *surface;
};
#endif

View File

@@ -1,132 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include "thread.h"
using namespace SDL;
Thread::Thread() : thread(NULL)
{
}
Thread::~Thread()
{
Kill();
}
void Thread::Create(int (*fn)(void *), void *param)
{
thread = SDL_CreateThread(fn, param);
}
int Thread::Wait(void)
{
int status = 0;
if(thread) SDL_WaitThread(thread, &status);
thread = NULL;
return status;
}
void Thread::Kill(void)
{
if(thread) SDL_KillThread(thread);
thread = NULL;
}
bool Thread::IsRun(void) const
{
return thread;
}
u32 Thread::GetID(void) const
{
return thread ? SDL_GetThreadID(thread) : 0;
}
Mutex::Mutex() : mutex(SDL_CreateMutex())
{
}
Mutex::~Mutex()
{
if(mutex) SDL_DestroyMutex(mutex);
}
bool Mutex::Lock(void) const
{
return mutex ? 0 == SDL_mutexP(mutex) : false;
}
bool Mutex::Unlock(void) const
{
return mutex ? 0 == SDL_mutexV(mutex) : false;
}
Timer::Timer() : id(0)
{
}
void Timer::Run(Timer & timer, u32 interval, u32 (*fn)(u32, void *), void *param)
{
if(timer.id) Remove(timer);
timer.id = SDL_AddTimer(interval, fn, param);
}
void Timer::Remove(Timer & timer)
{
if(timer.id)
{
SDL_RemoveTimer(timer.id);
timer.id = 0;
}
}
bool Timer::IsValid(void) const
{
return id;
}
Time::Time()
{
}
void Time::Start(void)
{
tick2 = tick1 = SDL_GetTicks();
}
void Time::Stop(void)
{
tick2 = SDL_GetTicks();
}
u32 Time::Get(void) const
{
return tick2 > tick1 ? tick2 - tick1 : 0;
}
void Time::Print(const char* header) const
{
std::cerr << (header ? header : "time: ") << Get() << " ms" << std::endl;
}

View File

@@ -1,95 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef SDLTHREAD_H
#define SDLTHREAD_H
#include <sys/time.h>
#include "SDL_thread.h"
#include "types.h"
namespace SDL
{
class Thread
{
public:
Thread();
~Thread();
void Create(int (*)(void *), void *param = NULL);
int Wait(void);
void Kill(void);
bool IsRun(void) const;
u32 GetID(void) const;
private:
SDL_Thread *thread;
};
class Mutex
{
public:
Mutex();
~Mutex();
bool Lock(void) const;
bool Unlock(void) const;
private:
SDL_mutex *mutex;
};
class Timer
{
public:
Timer();
bool IsValid(void) const;
static void Run(Timer &, u32, u32 (*)(u32, void *), void *param = NULL);
static void Remove(Timer &);
private:
SDL_TimerID id;
};
class Time
{
public:
Time();
void Start(void);
void Stop(void);
u32 Get(void) const;
void Print(const char* header = NULL) const;
private:
u32 tick1;
u32 tick2;
};
}
#endif

View File

@@ -1,269 +0,0 @@
/***************************************************************************
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of SDL++ Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <fstream>
#include <iterator>
#include <cctype>
#include <iostream>
#include <algorithm>
#include "tinyconfig.h"
#include "tools.h"
bool SpaceCompare(char a, char b)
{
return std::isspace(a) && std::isspace(b);
}
void ModifyKey(std::string & key)
{
String::Lower(key);
String::Trim(key);
// remove multiple space
std::string::iterator it = std::unique(key.begin(), key.end(), SpaceCompare);
key.resize(it - key.begin());
// change space
std::replace_if(key.begin(), key.end(), ::isspace, 0x20);
}
Tiny::Value::Value() : ival(0)
{
}
Tiny::Value::Value(int val)
{
ival = val;
sval = val;
}
Tiny::Value::Value(const char* val)
{
sval = val;
ival = String::ToInt(sval);
}
void Tiny::Value::operator= (int val)
{
ival = val;
sval.clear();
String::AddInt(sval, val);
}
void Tiny::Value::operator= (const char* val)
{
sval = val;
ival = String::ToInt(sval);
}
std::ostream & Tiny::operator<< (std::ostream & os, const Tiny::Entry & en)
{
os << en.first << " = " << en.second.sval << std::endl;
return os;
}
Tiny::Entry::Entry()
{
}
Tiny::Entry::Entry(const char* key, const char* val) :
std::pair<std::string, Value>(key, val)
{
ModifyKey(first);
}
Tiny::Entry::Entry(const char* key, int val) :
std::pair<std::string, Value>(key, val)
{
ModifyKey(first);
}
const std::string & Tiny::Entry::StrParams(void) const
{
return second.sval;
}
int Tiny::Entry::IntParams(void) const
{
return second.ival;
}
bool Tiny::Entry::IsKey(const char* key) const
{
return key && key == first;
}
bool Tiny::Entry::IsValue(const char* val) const
{
return val && val == second.sval;
}
bool Tiny::Entry::IsValue(int val) const
{
return val == second.ival;
}
Tiny::Config::Config() : separator('='), comment(';')
{
}
Tiny::Config::~Config()
{
}
void Tiny::Config::SetSeparator(char c)
{
separator = c;
}
void Tiny::Config::SetComment(char c)
{
comment = c;
}
bool Tiny::Config::Load(const char* cfile)
{
if(!cfile) return false;
std::ifstream fs(cfile);
if(!fs.is_open()) return false;
std::string str;
while(std::getline(fs, str))
{
String::Trim(str);
if(str.empty() || str[0] == comment) continue;
size_t pos = str.find(separator);
if(std::string::npos != pos)
{
std::string left(str.substr(0, pos));
std::string right(str.substr(pos + 1, str.length() - pos - 1));
String::Trim(left);
String::Trim(right);
AddEntry(left.c_str(), right.c_str(), false);
}
}
fs.close();
return true;
}
bool Tiny::Config::Save(const char* cfile)
{
if(!cfile) return false;
std::ofstream fs(cfile);
if(!fs.is_open()) return false;
Dump(fs);
fs.close();
return true;
}
void Tiny::Config::Dump(std::ostream & os)
{
std::copy(entries.begin(), entries.end(), std::ostream_iterator<Entry>(os, ""));
}
Tiny::EntryIterator Tiny::Config::FindEntry(std::string key)
{
ModifyKey(key);
return std::find_if(entries.begin(), entries.end(), std::bind2nd(std::mem_fun_ref(&Entry::IsKey), key.c_str()));
}
Tiny::EntryConstIterator Tiny::Config::FindEntry(std::string key) const
{
ModifyKey(key);
return std::find_if(entries.begin(), entries.end(), std::bind2nd(std::mem_fun_ref(&Entry::IsKey), key.c_str()));
}
int Tiny::Config::IntParams(const char* key) const
{
EntryConstIterator it = FindEntry(key);
return it != entries.end() ? (*it).second.ival : 0;
}
const char* Tiny::Config::StrParams(const char* key) const
{
EntryConstIterator it = FindEntry(key);
return it != entries.end() ? (*it).second.sval.c_str() : NULL;
}
void Tiny::Config::GetParams(const char* ckey, std::list<std::string> & res) const
{
std::string key(ckey);
ModifyKey(key);
for(EntryConstIterator it = entries.begin(); it != entries.end(); ++it)
if((*it).IsKey(ckey)) res.push_back((*it).second.sval);
}
const Tiny::Entry* Tiny::Config::Find(const char* key) const
{
EntryConstIterator it = FindEntry(key);
return it != entries.end() ? &(*it) : NULL;
}
void Tiny::Config::AddEntry(const char* key, const char* val, bool uniq)
{
if(uniq)
{
EntryIterator it = FindEntry(key);
if(it != entries.end())
(*it).second = val;
else
entries.push_back(Entry(key, val));
}
else
entries.push_back(Entry(key, val));
}
void Tiny::Config::AddEntry(const char* key, int val, bool uniq)
{
if(uniq)
{
EntryIterator it = FindEntry(key);
if(it != entries.end())
(*it).second = val;
else
entries.push_back(Entry(key, val));
}
else
entries.push_back(Entry(key, val));
}
void Tiny::Config::Clear(void)
{
entries.clear();
}
const Tiny::Entries & Tiny::Config::GetEntries(void) const
{
return entries;
}

View File

@@ -1,102 +0,0 @@
/***************************************************************************
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of SDL++ Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef TINYCONFIG_H
#define TINYCONFIG_H
#include <utility>
#include <ostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <list>
namespace Tiny
{
struct Value
{
Value();
Value(int);
Value(const char*);
void operator= (int);
void operator= (const char*);
int ival;
std::string sval;
};
struct Entry : public std::pair<std::string, Value>
{
Entry();
Entry(const char*, const char*);
Entry(const char*, int);
const std::string & StrParams(void) const;
int IntParams(void) const;
bool IsKey(const char*) const;
bool IsValue(const char*) const;
bool IsValue(int) const;
};
std::ostream & operator<< (std::ostream &, const Entry &);
typedef std::list<Entry> Entries;
typedef std::list<Entry>::iterator EntryIterator;
typedef std::list<Entry>::const_iterator EntryConstIterator;
class Config
{
public:
Config();
~Config();
bool Load(const char*);
bool Save(const char*);
void Dump(std::ostream &);
void Clear(void);
void SetSeparator(char);
void SetComment(char);
int IntParams(const char*) const;
const char* StrParams(const char*) const;
void GetParams(const char*, std::list<std::string> &) const;
void AddEntry(const char*, const char*, bool uniq = true);
void AddEntry(const char*, int, bool uniq = true);
const Entry* Find(const char*) const;
const Entries & GetEntries(void) const;
protected:
EntryIterator FindEntry(std::string);
EntryConstIterator FindEntry(std::string) const;
char separator;
char comment;
Entries entries;
};
}
#endif

View File

@@ -1,530 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <algorithm>
#include <sstream>
#include <fstream>
#include <iostream>
#include <cstring>
#include <climits>
#include "error.h"
#include "types.h"
#include "tools.h"
/* trim left right space */
void String::Trim(std::string & str)
{
std::string::iterator iter;
// left
iter = str.begin();
while(iter != str.end() && std::isspace(*iter)) ++iter;
if(iter != str.begin()) str.erase(str.begin(), iter);
// right
iter = str.end() - 1;
while(iter != str.begin() && std::isspace(*iter)) --iter;
if(iter != str.end() - 1) str.erase(iter + 1, str.end());
}
/* convert to lower case */
void String::Lower(std::string & str)
{
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
}
/* convert to upper case */
void String::Upper(std::string & str)
{
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}
/* int to string */
void String::AddInt(std::string &str, int value)
{
std::ostringstream stream;
stream << value;
str += stream.str();
}
int String::ToInt(const std::string & str)
{
int res = 0;
// decimal
if(str.end() == std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun<int, int>(std::isdigit))))
{
std::istringstream ss(str);
ss >> res;
}
else
// hex
if(str.size() > 3 && str.at(0) == '0' && std::tolower(str.at(1)) == 'x' &&
str.end() == std::find_if(str.begin() + 2, str.end(), std::not1(std::ptr_fun<int, int>(std::isxdigit))))
{
std::istringstream ss(str);
ss >> std::hex >> res;
}
else
// str
{
std::string lower(str);
String::Lower(lower);
if(lower == "on") return 1;
else
if(lower == "one") return 1;
else
if(lower == "two") return 2;
else
if(lower == "three") return 3;
else
if(lower == "four") return 4;
else
if(lower == "five") return 5;
else
if(lower == "six") return 6;
else
if(lower == "seven") return 7;
else
if(lower == "eight") return 8;
else
if(lower == "nine") return 9;
}
return res;
}
/* string compare */
bool String::Compare(const std::string &str1, const std::string &str2, bool sensitive)
{
if(str1.size() != str2.size()) return false;
if(sensitive) return str1 == str2;
std::string strl1(str1);
std::string strl2(str2);
Lower(strl1);
Lower(strl2);
return str1 == str2;
}
void String::Replace(std::string & dst, const char* pred, const char* src)
{
size_t pos = std::string::npos;
while(std::string::npos != (pos = dst.find(pred))) dst.replace(pos, std::strlen(pred), src);
}
void String::Replace(std::string & dst, const char* pred, const std::string & src)
{
size_t pos = std::string::npos;
while(std::string::npos != (pos = dst.find(pred))) dst.replace(pos, std::strlen(pred), src);
}
void String::Replace(std::string & dst, const char* pred, int value)
{
if(std::string::npos != dst.find(pred))
{
std::ostringstream stream;
stream << value;
Replace(dst, pred, stream.str());
}
}
// from SDL_ttf
void String::UTF8_to_UNICODE(u16 *unicode, const char *utf8, int len)
{
int i, j;
u16 ch;
for ( i=0, j=0; i < len; ++i, ++j )
{
ch = ((const unsigned char *)utf8)[i];
if ( ch >= 0xF0 )
{
ch = (u16)(utf8[i]&0x07) << 18;
ch |= (u16)(utf8[++i]&0x3F) << 12;
ch |= (u16)(utf8[++i]&0x3F) << 6;
ch |= (u16)(utf8[++i]&0x3F);
}
else
if ( ch >= 0xE0 )
{
ch = (u16)(utf8[i]&0x0F) << 12;
ch |= (u16)(utf8[++i]&0x3F) << 6;
ch |= (u16)(utf8[++i]&0x3F);
}
else
if ( ch >= 0xC0 )
{
ch = (u16)(utf8[i]&0x1F) << 6;
ch |= (u16)(utf8[++i]&0x3F);
}
unicode[j] = ch;
}
unicode[j] = 0;
}
void String::UNICODE_to_UTF8(std::string & utf8, const u16 *unicode, size_t len)
{
utf8.reserve(2 * len);
for(size_t ii = 0; ii < len; ++ii)
{
if(unicode[ii] < 128)
{
utf8.append(1, static_cast<char>(unicode[ii]));
}
else
if(unicode[ii] < 2048)
{
utf8.append(1, static_cast<char>(192 + ((unicode[ii] - (unicode[ii] % 64)) / 64)));
utf8.append(1, static_cast<char>(128 + (unicode[ii] % 64)));
}
else
{
utf8.append(1, static_cast<char>(224 + ((unicode[ii] - (unicode[ii] % 4096)) / 4096)));
utf8.append(1, static_cast<char>(128 + (((unicode[ii] % 4096) - (unicode[ii] % 64)) / 64)));
utf8.append(1, static_cast<char>(128 + (unicode[ii] % 64)));
}
}
}
void String::AppendKey(std::string & res, KeySym sym, u16 mod)
{
switch(sym)
{
case KEY_1: res += (MOD_SHIFT & mod ? '!' : '1'); break;
case KEY_2: res += (MOD_SHIFT & mod ? '@' : '2'); break;
case KEY_3: res += (MOD_SHIFT & mod ? '#' : '3'); break;
case KEY_4: res += (MOD_SHIFT & mod ? '$' : '4'); break;
case KEY_5: res += (MOD_SHIFT & mod ? '%' : '5'); break;
case KEY_6: res += (MOD_SHIFT & mod ? '^' : '6'); break;
case KEY_7: res += (MOD_SHIFT & mod ? '&' : '7'); break;
case KEY_8: res += (MOD_SHIFT & mod ? '*' : '8'); break;
case KEY_9: res += (MOD_SHIFT & mod ? '(' : '9'); break;
case KEY_0: res += (MOD_SHIFT & mod ? ')' : '0'); break;
case KEY_MINUS: res += (MOD_SHIFT & mod ? '_' : '-'); break;
case KEY_EQUALS: res += (MOD_SHIFT & mod ? '+' : '='); break;
case KEY_BACKSLASH: res += (MOD_SHIFT & mod ? '|' : '\\'); break;
case KEY_LEFTBRACKET: res += (MOD_SHIFT & mod ? '{' : '['); break;
case KEY_RIGHTBRACKET: res += (MOD_SHIFT & mod ? '}' : ']'); break;
case KEY_SEMICOLON: res += (MOD_SHIFT & mod ? ':' : ';'); break;
case KEY_QUOTE: res += (MOD_SHIFT & mod ? '"' : '\''); break;
case KEY_COMMA: res += (MOD_SHIFT & mod ? '<' : ','); break;
case KEY_PERIOD: res += (MOD_SHIFT & mod ? '>' : '.'); break;
case KEY_SLASH: res += (MOD_SHIFT & mod ? '?' : '/'); break;
case KEY_EXCLAIM: res += '!'; break;
case KEY_AT: res += '@'; break;
case KEY_HASH: res += '#'; break;
case KEY_DOLLAR: res += '$'; break;
case KEY_AMPERSAND: res += '&'; break;
case KEY_ASTERISK: res += '*'; break;
case KEY_LEFTPAREN: res += '('; break;
case KEY_RIGHTPAREN: res += ')'; break;
case KEY_QUOTEDBL: res += '"'; break;
case KEY_PLUS: res += '+'; break;
case KEY_COLON: res += ':'; break;
case KEY_LESS: res += '<'; break;
case KEY_GREATER: res += '>'; break;
case KEY_QUESTION: res += '?'; break;
case KEY_CARET: res += '^'; break;
case KEY_UNDERSCORE: res += '_'; break;
case KEY_SPACE: res += ' '; break;
case KEY_a: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'A' : 'a'); break;
case KEY_b: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'B' : 'b'); break;
case KEY_c: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'C' : 'c'); break;
case KEY_d: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'D' : 'd'); break;
case KEY_e: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'E' : 'e'); break;
case KEY_f: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'F' : 'f'); break;
case KEY_g: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'G' : 'g'); break;
case KEY_h: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'H' : 'h'); break;
case KEY_i: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'I' : 'i'); break;
case KEY_j: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'J' : 'j'); break;
case KEY_k: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'K' : 'k'); break;
case KEY_l: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'L' : 'l'); break;
case KEY_m: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'M' : 'm'); break;
case KEY_n: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'N' : 'n'); break;
case KEY_o: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'O' : 'o'); break;
case KEY_p: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'P' : 'p'); break;
case KEY_q: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'Q' : 'q'); break;
case KEY_r: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'R' : 'r'); break;
case KEY_s: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'S' : 's'); break;
case KEY_t: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'T' : 't'); break;
case KEY_u: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'U' : 'u'); break;
case KEY_v: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'V' : 'v'); break;
case KEY_w: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'W' : 'w'); break;
case KEY_x: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'X' : 'x'); break;
case KEY_y: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'Y' : 'y'); break;
case KEY_z: res += ((MOD_SHIFT | MOD_CAPS) & mod ? 'Z' : 'z'); break;
case KEY_BACKSPACE: if(res.size()) res.resize(res.size() - 1); break;
default: break;
}
}
int Sign(int s)
{
return (s < 0 ? -1 : (s > 0 ? 1 : 0));
}
std::string GetDirname(const std::string & str)
{
if(str.size())
{
size_t pos = str.rfind(SEPARATOR);
if(std::string::npos == pos)
return std::string(".");
else
if(pos == 0)
return str;
else
if(pos == str.size() - 1)
return GetDirname(str.substr(0, str.size() - 1));
else
return str.substr(0, pos);
}
return str;
}
std::string GetBasename(const std::string & str)
{
if(str.size())
{
size_t pos = str.rfind(SEPARATOR);
if(std::string::npos == pos ||
pos == 0) return str;
else
if(pos == str.size() - 1)
return GetBasename(str.substr(0, str.size() - 1));
else
return str.substr(pos + 1);
}
return str;
}
#if defined __SYMBIAN32__
u32 GetMemoryUsage(void)
{
return 0;
}
#elif defined __WIN32__
#include "windows.h"
u32 GetMemoryUsage(void)
{
static MEMORYSTATUS ms;
ZeroMemory(&ms, sizeof(ms));
ms.dwLength = sizeof(MEMORYSTATUS);
GlobalMemoryStatus(&ms);
return (ms.dwTotalVirtual - ms.dwAvailVirtual);
}
#elif defined __LINUX__
#include "unistd.h"
u32 GetMemoryUsage(void)
{
unsigned int size = 0;
std::ostringstream os;
os << "/proc/" << getpid() << "/statm";
std::ifstream fs(os.str().c_str());
if(fs.is_open())
{
fs >> size;
fs.close();
}
return size * getpagesize();
}
#else
u32 GetMemoryUsage(void)
{
return 0;
}
#endif
KeySym KeySymFromChar(char c)
{
switch(c)
{
case '!': return KEY_EXCLAIM;
case '"': return KEY_QUOTEDBL;
case '#': return KEY_HASH;
case '$': return KEY_DOLLAR;
case '&': return KEY_AMPERSAND;
case '\'': return KEY_QUOTE;
case '(': return KEY_LEFTPAREN;
case ')': return KEY_RIGHTPAREN;
case '*': return KEY_ASTERISK;
case '+': return KEY_PLUS;
case ',': return KEY_COMMA;
case '-': return KEY_MINUS;
case '.': return KEY_PERIOD;
case '/': return KEY_SLASH;
case ':': return KEY_COLON;
case ';': return KEY_SEMICOLON;
case '<': return KEY_LESS;
case '=': return KEY_EQUALS;
case '>': return KEY_GREATER;
case '?': return KEY_QUESTION;
case '@': return KEY_AT;
case '[': return KEY_LEFTBRACKET;
case '\\': return KEY_BACKSLASH;
case ']': return KEY_RIGHTBRACKET;
case '^': return KEY_CARET;
case '_': return KEY_UNDERSCORE;
case ' ': return KEY_SPACE;
case 'a': return KEY_a;
case 'b': return KEY_b;
case 'c': return KEY_c;
case 'd': return KEY_d;
case 'e': return KEY_e;
case 'f': return KEY_f;
case 'g': return KEY_g;
case 'h': return KEY_h;
case 'i': return KEY_i;
case 'j': return KEY_j;
case 'k': return KEY_k;
case 'l': return KEY_l;
case 'm': return KEY_m;
case 'n': return KEY_n;
case 'o': return KEY_o;
case 'p': return KEY_p;
case 'q': return KEY_q;
case 'r': return KEY_r;
case 's': return KEY_s;
case 't': return KEY_t;
case 'u': return KEY_u;
case 'v': return KEY_v;
case 'w': return KEY_w;
case 'x': return KEY_x;
case 'y': return KEY_y;
case 'z': return KEY_z;
case '0': return KEY_0;
case '1': return KEY_1;
case '2': return KEY_2;
case '3': return KEY_3;
case '4': return KEY_4;
case '5': return KEY_5;
case '6': return KEY_6;
case '7': return KEY_7;
case '8': return KEY_8;
case '9': return KEY_9;
default: break;
}
return KEY_NONE;
}
bool FilePresent(const std::string & file)
{
std::ifstream fs;
// check file
fs.open(file.c_str(), std::ios::binary);
if(fs.is_open())
{
fs.close();
return true;
}
return false;
}
bool StoreMemToFile(const std::vector<u8> & data, const std::string & file)
{
std::ofstream fs;
fs.open(file.c_str(), std::ios::binary);
if(fs.is_open() && data.size())
{
fs.write(reinterpret_cast<const char*>(&data[0]), data.size());
fs.close();
return true;
}
return false;
}
bool StoreFileToMem(std::vector<u8> & data, const std::string & file)
{
std::ifstream fs;
fs.open(file.c_str(), std::ios::binary);
if(fs.is_open())
{
fs.seekg(0, std::ios_base::end);
data.resize(fs.tellg());
fs.seekg(0, std::ios_base::beg);
fs.read(reinterpret_cast<char*>(&data[0]), data.size());
fs.close();
return true;
}
return false;
}
bool PressIntKey(u32 min, u32 max, u32 & result)
{
LocalEvent & le = LocalEvent::Get();
if(le.KeyPress(KEY_BACKSPACE))
{
if(min < result)
{
result /= 10;
if(result < min) result = min;
}
return true;
}
else
if(le.KeyPress() && KEY_0 <= le.KeyValue() && KEY_9 >= le.KeyValue())
{
if(max > result)
{
result *= 10;
switch(le.KeyValue())
{
case KEY_1: result += 1; break;
case KEY_2: result += 2; break;
case KEY_3: result += 3; break;
case KEY_4: result += 4; break;
case KEY_5: result += 5; break;
case KEY_6: result += 6; break;
case KEY_7: result += 7; break;
case KEY_8: result += 8; break;
case KEY_9: result += 9; break;
default: break;
}
if(result > max) result = max;
}
return true;
}
return false;
}

View File

@@ -1,63 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2TOOLS_H
#define H2TOOLS_H
#include <string>
#include "localevent.h"
#include "types.h"
namespace String
{
void Trim(std::string &str);
void Lower(std::string &str);
void Upper(std::string &str);
void AddInt(std::string &str, int value);
int ToInt(const std::string &str);
bool Compare(const std::string &str1, const std::string &str2, bool sensitive = true);
void Replace(std::string &, const char*, const char *);
void Replace(std::string &, const char*, const std::string &);
void Replace(std::string &, const char*, int);
void AppendKey(std::string &, KeySym, u16);
// from SDL_ttf
void UTF8_to_UNICODE(u16 *unicode, const char *utf8, int len);
void UNICODE_to_UTF8(std::string & utf8, const u16 *unicode, size_t len);
}
int Sign(int);
KeySym KeySymFromChar(char);
bool PressIntKey(u32 min, u32 max, u32 & result);
std::string GetDirname(const std::string &);
std::string GetBasename(const std::string &);
u32 GetMemoryUsage(void);
bool StoreMemToFile(const std::vector<u8> &, const std::string &);
bool StoreFileToMem(std::vector<u8> &, const std::string &);
bool FilePresent(const std::string &);
#endif

View File

@@ -1,71 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "types.h"
u32 ReadBE32(const u8 *p)
{
return ((((u32) *p) << 24) | (((u32) *(p + 1)) << 16) | (((u32) *(p + 2)) << 8) | ((u32) *(p + 3)));
}
u32 ReadLE32(const u8 *p)
{
return ((((u32) *(p + 3)) << 24) | (((u32) *(p + 2)) << 16) | (((u32) *(p + 1)) << 8) | ((u32) *p));
}
u16 ReadBE16(const u8 *p)
{
return((((u16) *p) << 8) | ((u16) *(p + 1)));
}
u16 ReadLE16(const u8 *p)
{
return((((u16) *(p + 1)) << 8) | ((u16) *p));
}
void WriteBE32(u8 *p, u32 x)
{
*p = static_cast<u8>(x >> 24);
*(p + 1) = static_cast<u8>((x & 0x00FF0000) >> 16);
*(p + 2) = static_cast<u8>((x & 0x0000FF00) >> 8);
*(p + 3) = static_cast<u8>(x & 0x000000FF);
}
void WriteBE16(u8 *p, u16 x)
{
*p = static_cast<u8>(x >> 8);
*(p + 1) = static_cast<u8>(x & 0x00FF);
}
void WriteLE32(u8 *p, u32 x)
{
*(p + 3) = static_cast<u8>(x >> 24);
*(p + 2) = static_cast<u8>((x & 0x00FF0000) >> 16);
*(p + 1) = static_cast<u8>((x & 0x0000FF00) >> 8);
*p = static_cast<u8>(x & 0x000000FF);
}
void WriteLE16(u8 *p, u16 x)
{
*(p + 1) = static_cast<u8>(x >> 8);
*p = static_cast<u8>(x & 0x00FF);
}

View File

@@ -1,137 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2TYPES_H
#define H2TYPES_H
#include "SDL.h"
typedef Sint8 s8;
typedef Uint8 u8;
typedef Sint16 s16;
typedef Uint16 u16;
typedef Sint32 s32;
typedef Uint32 u32;
typedef SDL_Color Colors;
#define MAXU16 0xFFFF
#define MAXU32 0xFFFFFFFF
#if defined __SYMBIAN32__
#define MKDIR(X) mkdir(X, S_IRWXU)
#define SEPARATOR '\\'
#elif defined __WIN32__
#include <io.h>
#define MKDIR(X) mkdir(X)
#define SEPARATOR '\\'
#else
#define MKDIR(X) mkdir(X, S_IRWXU)
#define SEPARATOR '/'
#endif
#define DELAY(X) SDL_Delay(X)
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define RMASK16 0x0000f000
#define GMASK16 0x00000f00
#define BMASK16 0x000000f0
#define AMASK16 0x0000000f
#define RMASK24 0x00fc0000
#define GMASK24 0x0003f000
#define BMASK24 0x00000fc0
#define AMASK24 0x0000003f
#define RMASK32 0xff000000
#define GMASK32 0x00ff0000
#define BMASK32 0x0000ff00
#define AMASK32 0x000000ff
#else
#define RMASK16 0x0000000f
#define GMASK16 0x000000f0
#define BMASK16 0x00000f00
#define AMASK16 0x0000f000
#define RMASK24 0x0000003f
#define GMASK24 0x00000fc0
#define BMASK24 0x0003f000
#define AMASK24 0x00fc0000
#define RMASK32 0x000000ff
#define GMASK32 0x0000ff00
#define BMASK32 0x00ff0000
#define AMASK32 0xff000000
#endif
#define Swap16(X) X=SDL_Swap16(X)
#define Swap32(X) X=SDL_Swap32(X)
#define SwapLE16(X) X=SDL_SwapLE16(X)
#define SwapLE32(X) X=SDL_SwapLE32(X)
#define SwapBE16(X) X=SDL_SwapBE16(X)
#define SwapBE32(X) X=SDL_SwapBE32(X)
u32 ReadBE32(const u8 *p);
u32 ReadLE32(const u8 *p);
u16 ReadBE16(const u8 *p);
u16 ReadLE16(const u8 *p);
void WriteBE32(u8 *p, u32 x);
void WriteBE16(u8 *p, u16 x);
void WriteLE32(u8 *p, u32 x);
void WriteLE16(u8 *p, u16 x);
#if defined __SYMBIAN32__
#define PATH_MAX FILENAME_MAX
namespace std
{
int c_abs(int x);
float c_abs(float x);
double c_abs(double x);
int c_isspace(char c);
#define isspace(c) c_isspace(c)
#define abs(x) c_abs(x)
}
using namespace std;
#endif
#if defined __MINGW32CE__
#include <cstdlib>
#ifndef PATH_MAX
#define PATH_MAX 255
#endif
#define setlocale(x,y) 0
#define system(x) 0
#define putenv(x) SDL_putenv(x)
#define getenv(x) SDL_getenv(x)
#define getopt(x, y, z) -1
#define optarg 0
#else
#include <cstdlib>
#endif
#endif

View File

@@ -1,94 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "zzlib.h"
#ifdef WITH_ZLIB
#include <zlib.h>
#include <iostream>
bool ZLib::UnCompress(std::vector<char> & dst, const char* src, size_t srcsz, bool debug)
{
if(src && srcsz)
{
uLong dstsz = srcsz * 20;
dst.resize(dstsz);
int res = 0;
while(Z_BUF_ERROR == (res = uncompress(reinterpret_cast<Bytef*>(&dst[0]), &dstsz, reinterpret_cast<const Bytef*>(src), srcsz)))
{ dstsz = dst.size() * 2; dst.resize(dstsz); }
dst.resize(dstsz);
switch(res)
{
case Z_OK: return true;
case Z_MEM_ERROR: if(debug) std::cerr << "ZLib::UnCompress: " << "Z_MEM_ERROR" << std::endl; return false;
case Z_BUF_ERROR: if(debug) std::cerr << "ZLib::UnCompress: " << "Z_BUF_ERROR" << std::endl; return false;
case Z_DATA_ERROR:if(debug) std::cerr << "ZLib::UnCompress: " << "Z_DATA_ERROR"<< std::endl; return false;
default: break;
}
return Z_OK == res;
}
return false;
}
bool ZLib::UnCompress(std::vector<char> & dst, const std::vector<char> & src, bool debug)
{
return src.size() && UnCompress(dst, &src[0], src.size(), debug);
}
bool ZLib::UnCompress(std::vector<char> & dst, const std::string & src, bool debug)
{
return src.size() && UnCompress(dst, src.c_str(), src.size(), debug);
}
bool ZLib::Compress(std::vector<char> & dst, const char* src, size_t srcsz)
{
if(src && srcsz)
{
dst.resize(compressBound(srcsz));
uLong dstsz = dst.size();
int res = compress(reinterpret_cast<Bytef*>(&dst[0]), &dstsz, reinterpret_cast<const Bytef*>(src), srcsz);
dst.resize(dstsz);
return Z_OK == res;
}
return false;
}
bool ZLib::Compress(std::vector<char> & dst, const std::vector<char> & src)
{
return src.size() && Compress(dst, &src[0], src.size());
}
bool ZLib::Compress(std::vector<char> & dst, const std::string & src)
{
return src.size() && Compress(dst, src.c_str(), src.size());
}
bool ZSurface::Load(u16 w, u16 h, u8 b, const u8* p, size_t s, bool a)
{
if(!ZLib::UnCompress(buf, reinterpret_cast<const char*>(p), s)) return false;
Set(&buf[0], w, h, b, a);
return true;
}
#endif

View File

@@ -1,56 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2ZLIB_H
#define H2ZLIB_H
#ifdef WITH_ZLIB
#include <string>
#include <vector>
#include "types.h"
#include "surface.h"
namespace ZLib
{
bool UnCompress(std::vector<char> &, const char*, size_t, bool debug = false);
bool UnCompress(std::vector<char> &, const std::vector<char> &, bool debug = false);
bool UnCompress(std::vector<char> &, const std::string &, bool debug = false);
bool Compress(std::vector<char> &, const char*, size_t);
bool Compress(std::vector<char> &, const std::vector<char> &);
bool Compress(std::vector<char> &, const std::string &);
}
class ZSurface : public Surface
{
public:
ZSurface(){}
bool Load(u16 w, u16 h, u8 b, const u8* p, size_t s, bool a);
private:
std::vector<char> buf;
};
#endif
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,208 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2AGG_H
#define H2AGG_H
#include <string>
#include <list>
#include <vector>
#include <map>
#include "gamedefs.h"
#include "icn.h"
#include "til.h"
#include "xmi.h"
#include "m82.h"
#include "mus.h"
#include "sprite.h"
#include "font.h"
namespace AGG
{
class FAT
{
public:
FAT() : crc(0), offset(0), size(0) {}
u32 crc;
u32 offset;
u32 size;
void Dump(const std::string & n) const;
};
class File
{
public:
File();
~File();
bool Open(const std::string &);
bool isGood(void) const;
const std::string & Name(void) const;
const FAT & Fat(const std::string & key);
u16 CountItems(void);
bool Read(const std::string & key, std::vector<u8> & body);
void Dump(void) const;
private:
std::string filename;
std::map<std::string, FAT> fat;
u16 count_items;
std::ifstream* stream;
};
struct icn_cache_t
{
icn_cache_t() : sprites(NULL), reflect(NULL), count(0) {}
Sprite *sprites;
Sprite *reflect;
u16 count;
};
struct til_cache_t
{
til_cache_t() : sprites(NULL), count(0) {}
Surface *sprites;
u16 count;
};
struct fnt_cache_t
{
fnt_cache_t() : medium_white(NULL), medium_yellow(NULL), small_white(NULL), small_yellow(NULL) {}
Surface medium_white;
Surface medium_yellow;
Surface small_white;
Surface small_yellow;
};
struct loop_sound_t
{
loop_sound_t(M82::m82_t w, int c) : sound(w), channel(c) {}
bool isM82(const M82::m82_t wav) const{ return wav == sound; }
M82::m82_t sound;
int channel;
};
class Cache
{
public:
~Cache();
static Cache & Get(void);
bool ReadDataDir(void);
bool ReadChunk(const std::string & key, std::vector<u8> & body);
int GetICNCount(const ICN::icn_t icn);
const Sprite & GetICN(const ICN::icn_t icn, u16 index, bool reflect = false);
const Surface & GetTIL(const TIL::til_t til, u16 index, u8 shape);
const std::vector<u8> & GetWAV(const M82::m82_t m82);
const std::vector<u8> & GetMID(const XMI::xmi_t xmi);
#ifdef WITH_TTF
const Surface & GetFNT(u16, u8);
const SDL::Font & GetMediumFont(void) const;
const SDL::Font & GetSmallFont(void) const;
void LoadFNT(u16);
#endif
void LoadExtICN(icn_cache_t &, const ICN::icn_t, const u16, bool);
bool LoadAltICN(icn_cache_t &, const std::string &, const u16, bool);
void LoadOrgICN(Sprite &, const ICN::icn_t, const u16, bool);
void LoadOrgICN(icn_cache_t &, const ICN::icn_t, const u16, bool);
void LoadICN(const ICN::icn_t icn, u16 index, bool reflect = false);
void LoadTIL(const TIL::til_t til);
void LoadWAV(const M82::m82_t m82);
void LoadMID(const XMI::xmi_t xmi);
void LoadLOOPXXSounds(const u16*);
void ResetMixer(void);
void LoadPAL(void);
void LoadFNT(void);
bool isValidFonts(void) const;
void FreeICN(const ICN::icn_t icn);
void FreeTIL(const TIL::til_t til);
void FreeWAV(const M82::m82_t m82);
void FreeMID(const XMI::xmi_t xmi);
void ClearAllICN(void);
void ClearAllWAV(void);
void ClearAllMID(void);
void ICNRegistryEnable(bool);
void ICNRegistryFreeObjects(void);
void Dump(void) const;
private:
Cache();
File heroes2_agg;
File heroes2x_agg;
icn_cache_t* icn_cache;
til_cache_t* til_cache;
std::vector<loop_sound_t> loop_sounds;
std::map<M82::m82_t, std::vector<u8> > wav_cache;
std::map<XMI::xmi_t, std::vector<u8> > mid_cache;
#ifdef WITH_TTF
std::map<u16, fnt_cache_t> fnt_cache;
SDL::Font font_medium;
SDL::Font font_small;
#endif
std::vector<ICN::icn_t> icn_registry;
bool icn_registry_enable;
};
// wrapper AGG::PreloadObject
void PreloadObject(const ICN::icn_t icn, bool reflect = false);
void PreloadObject(const TIL::til_t til);
// wrapper AGG::FreeObject
void FreeObject(const ICN::icn_t icn);
void FreeObject(const TIL::til_t til);
// wrapper AGG::GetXXX
void ICNRegistryEnable(bool);
void ICNRegistryFreeObjects(void);
int GetICNCount(const ICN::icn_t icn);
const Sprite & GetICN(const ICN::icn_t icn, const u16 index, bool reflect = false);
const Surface & GetTIL(const TIL::til_t til, const u16 index, const u8 shape);
const Surface & GetLetter(char ch, u8 ft);
#ifdef WITH_TTF
const Surface & GetUnicodeLetter(u16 ch, u8 ft);
#endif
// wrapper Audio
void PlaySound(const M82::m82_t m82);
void PlayMusic(const MUS::mus_t mus, bool loop = true);
}
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,950 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2ICN_H
#define H2ICN_H
#include "gamedefs.h"
namespace ICN
{
enum icn_t
{
ADVBORDE,
ADVBORD,
ADVBTNS,
ADVEBTNS,
ADVMCO,
AELEM,
APANBKGE,
APANBKG,
APANELE,
APANEL,
ARCHER2,
ARCHER,
ARCH_MSL,
ART32,
ARTFX,
ARTIFACT,
BARB32,
B_BFLG32,
BERZERK,
B_FLAG32,
BIGBAR,
BLDGXTRA,
BLESS,
BLIND,
BLUEFIRE,
BOAR,
BOAT32,
BOATSHAD,
BOATWIND,
BOOK,
BORDEDIT,
BOULDER,
BRCREST,
BROTHERS,
BTNBAUD,
BTNCMPGN,
BTNCOM,
BTNDCCFG,
BTNDC,
BTNEMAIN,
BTNENEW,
BTNESIZE,
BTNHOTST,
BTNMCFG,
BTNMODEM,
BTNMP,
BTNNET2,
BTNNET,
BTNNEWGM,
BTNSHNGL,
BUILDING,
BUYBUILD,
BUYBUILE,
CAMPBKGE,
CAMPBKGG,
CAMPXTRE,
CAMPXTRG,
CAPTCOVR,
CASLBAR,
CASLWIND,
CASLXTRA,
CASTBKGB,
CASTBKGK,
CASTBKGN,
CASTBKGS,
CASTBKGW,
CASTBKGZ,
CASTLEB,
CASTLEK,
CASTLEN,
CASTLES,
CASTLEW,
CASTLEZ,
CATAPULT,
CAVALRYB,
CAVALRYR,
CBKGBEAC,
CBKGCRCK,
CBKGDIMT,
CBKGDITR,
CBKGDSRT,
CBKGGRAV,
CBKGGRMT,
CBKGGRTR,
CBKGLAVA,
CBKGSNMT,
CBKGSNTR,
CBKGSWMP,
CBKGWATR,
CELLWIN,
CENTAUR,
CFLGSMAL,
CLOP32,
CLOUDLUK,
CMBTCAPB,
CMBTCAPK,
CMBTCAPN,
CMBTCAPS,
CMBTCAPW,
CMBTCAPZ,
CMBTFLE1,
CMBTFLE2,
CMBTFLE3,
CMBTHROB,
CMBTHROK,
CMBTHRON,
CMBTHROS,
CMBTHROW,
CMBTHROZ,
CMBTLOS1,
CMBTLOS2,
CMBTLOS3,
CMBTMISC,
CMBTSURR,
CMSECO,
COBJ0000,
COBJ0001,
COBJ0002,
COBJ0003,
COBJ0004,
COBJ0005,
COBJ0006,
COBJ0007,
COBJ0008,
COBJ0009,
COBJ0010,
COBJ0011,
COBJ0012,
COBJ0013,
COBJ0014,
COBJ0015,
COBJ0016,
COBJ0017,
COBJ0018,
COBJ0019,
COBJ0020,
COBJ0021,
COBJ0022,
COBJ0023,
COBJ0024,
COBJ0025,
COBJ0026,
COBJ0027,
COBJ0028,
COBJ0029,
COBJ0030,
COBJ0031,
COLDRAY,
COLDRING,
CONGRATS,
COVR0001,
COVR0002,
COVR0003,
COVR0004,
COVR0005,
COVR0006,
COVR0007,
COVR0008,
COVR0009,
COVR0010,
COVR0011,
COVR0012,
COVR0013,
COVR0014,
COVR0015,
COVR0016,
COVR0017,
COVR0018,
COVR0019,
COVR0020,
COVR0021,
COVR0022,
COVR0023,
COVR0024,
CPANBKGE,
CPANBKG,
CPANELE,
CPANEL,
CREST,
CSPANBKE,
CSPANBKG,
CSPANBTE,
CSPANBTN,
CSPANEL,
CSTLBARB,
CSTLCAPB,
CSTLCAPK,
CSTLCAPN,
CSTLCAPS,
CSTLCAPW,
CSTLCAPZ,
CSTLKNGT,
CSTLNECR,
CSTLSORC,
CSTLWRLK,
CSTLWZRD,
CTRACK00,
CTRACK01,
CTRACK02,
CTRACK03,
CTRACK04,
CTRACK05,
CTRACK06,
CURSE,
CYCLOPS,
DISRRAY,
DRAGBLAK,
DRAGBONE,
DRAGGREE,
DRAGRED,
DRAGSLAY,
DROPLISL,
DROPLIST,
DRUID2,
DRUID,
DRUIDMSL,
DUMMY,
DWARF2,
DWARF,
ECPANEL,
EDITBTNS,
EDITOR,
EDITPANL,
EELEM,
ELECTRIC,
ELF2,
ELF,
ELF__MSL,
ESCROLL,
ESPANBKG,
ESPANBTN,
ESPANEL,
EVIW_ALL,
EVIWDDOR,
EVIWHROS,
EVIWMINE,
EVIWPUZL,
EVIWRSRC,
EVIWRTFX,
EVIWTWNS,
EVIWWRLD,
EXPMRL,
EXTRAOVR,
FELEM,
FIREBAL2,
FIREBALL,
FLAG32,
FONT,
FRNG0001,
FRNG0002,
FRNG0003,
FRNG0004,
FRNG0005,
FRNG0006,
FRNG0007,
FRNG0008,
FRNG0009,
FRNG0010,
FRNG0011,
FRNG0012,
FRNG0013,
FROTH,
GARGOYLE,
G_BFLG32,
GENIE,
G_FLAG32,
GHOST,
GOBLIN,
GOLEM2,
GOLEM,
GRIFFIN,
GROUND12,
GROUND4,
GROUND6,
HALFLING,
HALFLMSL,
HASTE,
HEROBKG,
HEROES,
HEROEXTE,
HEROEXTG,
HEROFL00,
HEROFL01,
HEROFL02,
HEROFL03,
HEROFL04,
HEROFL05,
HEROFL06,
HEROLOGE,
HEROLOGO,
HISCORE,
HOURGLAS,
HSBKG,
HSBTNS,
HSICONS,
HYDRA,
HYPNOTIZ,
ICECLOUD,
KEEP,
KNGT32,
LETTER12,
LETTER4,
LETTER6,
LGNDXTRA,
LGNDXTRE,
LICH2,
LICHCLOD,
LICH,
LICH_MSL,
LISTBOX,
LISTBOXS,
LOCATORE,
LOCATORS,
MAGE1,
MAGE2,
MAGEGLDB,
MAGEGLDK,
MAGEGLDN,
MAGEGLDS,
MAGEGLDW,
MAGEGLDZ,
MAGIC01,
MAGIC02,
MAGIC03,
MAGIC04,
MAGIC06,
MAGIC07,
MAGIC08,
MANA,
MEDUSA,
METEOR,
MINICAPT,
MINIHERO,
MINILKMR,
MINIMON,
MINIPORT,
MINISS,
MINITOWN,
MINOTAU2,
MINOTAUR,
MISC12,
MISC4,
MISC6,
MOATPART,
MOATWHOL,
MOBILITY,
MONH0000,
MONH0001,
MONH0002,
MONH0003,
MONH0004,
MONH0005,
MONH0006,
MONH0007,
MONH0008,
MONH0009,
MONH0010,
MONH0011,
MONH0012,
MONH0013,
MONH0014,
MONH0015,
MONH0016,
MONH0017,
MONH0018,
MONH0019,
MONH0020,
MONH0021,
MONH0022,
MONH0023,
MONH0024,
MONH0025,
MONH0026,
MONH0027,
MONH0028,
MONH0029,
MONH0030,
MONH0031,
MONH0032,
MONH0033,
MONH0034,
MONH0035,
MONH0036,
MONH0037,
MONH0038,
MONH0039,
MONH0040,
MONH0041,
MONH0042,
MONH0043,
MONH0044,
MONH0045,
MONH0046,
MONH0047,
MONH0048,
MONH0049,
MONH0050,
MONH0051,
MONH0052,
MONH0053,
MONH0054,
MONH0055,
MONH0056,
MONH0057,
MONH0058,
MONH0059,
MONH0060,
MONH0061,
MONH0062,
MONH0063,
MONH0064,
MONH0065,
MONS32,
MORALEB,
MORALEG,
MTNCRCK,
MTNDIRT,
MTNDSRT,
MTNGRAS,
MTNLAVA,
MTNMULT,
MTNSNOW,
MTNSWMP,
MUMMY2,
MUMMYW,
NECR32,
NETBOX,
NGEXTRA,
NGHSBKG,
NGMPBKG,
NGSPBKG,
NOMAD,
O_BFLG32,
OBJNARTI,
OBJNCRCK,
OBJNDIRT,
OBJNDSRT,
OBJNGRA2,
OBJNGRAS,
OBJNHAUN,
OBJNLAV2,
OBJNLAV3,
OBJNLAVA,
OBJNMUL2,
OBJNMULT,
OBJNRSRC,
OBJNSNOW,
OBJNSWMP,
OBJNTOWN,
OBJNTWBA,
OBJNTWRD,
OBJNTWSH,
OBJNWAT2,
OBJNWATR,
OBJNXTRA,
OBJPALET,
O_FLAG32,
OGRE2,
OGRE,
ORC2,
ORC,
ORC__MSL,
OVERBACK,
OVERLAY,
OVERVIEW,
PALADIN2,
PALADIN,
PARALYZE,
P_BFLG32,
PEASANT,
P_FLAG32,
PHOENIX,
PHYSICAL,
PIKEMAN2,
PIKEMAN,
PORT0000,
PORT0001,
PORT0002,
PORT0003,
PORT0004,
PORT0005,
PORT0006,
PORT0007,
PORT0008,
PORT0009,
PORT0010,
PORT0011,
PORT0012,
PORT0013,
PORT0014,
PORT0015,
PORT0016,
PORT0017,
PORT0018,
PORT0019,
PORT0020,
PORT0021,
PORT0022,
PORT0023,
PORT0024,
PORT0025,
PORT0026,
PORT0027,
PORT0028,
PORT0029,
PORT0030,
PORT0031,
PORT0032,
PORT0033,
PORT0034,
PORT0035,
PORT0036,
PORT0037,
PORT0038,
PORT0039,
PORT0040,
PORT0041,
PORT0042,
PORT0043,
PORT0044,
PORT0045,
PORT0046,
PORT0047,
PORT0048,
PORT0049,
PORT0050,
PORT0051,
PORT0052,
PORT0053,
PORT0054,
PORT0055,
PORT0056,
PORT0057,
PORT0058,
PORT0059,
PORT0060,
PORT0061,
PORT0062,
PORT0063,
PORT0064,
PORT0065,
PORT0066,
PORT0067,
PORT0068,
PORT0069,
PORT0070,
PORT0090,
PORT0091,
PORT0092,
PORT0093,
PORT0094,
PORT0095,
PORTCFLG,
PORTMEDI,
PORTXTRA,
PRIMSKIL,
PUZZLE,
QWIKHERO,
QWIKINFO,
QWIKTOWN,
RADAR,
R_BFLG32,
RECR2BKG,
RECRBKG,
RECRUIT,
REDBACK,
REDDEATH,
REDFIRE,
REQBKG,
REQSBKG,
REQUEST,
REQUESTS,
RESOURCE,
RESSMALL,
R_FLAG32,
ROAD,
ROC,
ROGUE,
ROUTE,
SCENIBKG,
SCROLL2,
SCROLLCN,
SCROLLE,
SCROLL,
SECSKILL,
SHADOW32,
SHIELD,
SHNGANIM,
SKELETON,
SMALCLOD,
SMALFONT,
SMALLBAR,
SORC32,
SPANBKGE,
SPANBKG,
SPANBTNE,
SPANBTN,
SPANEL,
SPARKS,
SPELCO,
SPELLINF,
SPELLINL,
SPELLS,
SPRITE,
STELSKIN,
STONBACK,
STONBAKE,
STONEBAK,
STONEBK2,
STONSKIN,
STORM,
STREAM,
STRIP,
SUNMOONE,
SUNMOON,
SURDRBKE,
SURDRBKG,
SURRENDE,
SURRENDR,
SWAPBTN,
SWAPWIN,
SWORDSM2,
SWORDSMN,
SYSTEME,
SYSTEM,
TAVWIN,
TENT,
TERRAINS,
TEXTBACK,
TEXTBAK2,
TEXTBAR,
TITANBLA,
TITANBLU,
TITANMSL,
TOWNBKG0,
TOWNBKG1,
TOWNBKG2,
TOWNBKG3,
TOWNBKG4,
TOWNBKG5,
TOWNFIX,
TOWNNAME,
TOWNWIND,
TRADPOSE,
TRADPOST,
TREASURY,
TREDECI,
TREEVIL,
TREFALL,
TREFIR,
TREJNGL,
TRESNOW,
TROLL2,
TROLL,
TROLLMSL,
TWNBBOAT,
TWNBCAPT,
TWNBCSTL,
TWNBDOCK,
TWNBDW_0,
TWNBDW_1,
TWNBDW_2,
TWNBDW_3,
TWNBDW_4,
TWNBDW_5,
TWNBEXT0,
TWNBEXT1,
TWNBEXT2,
TWNBEXT3,
TWNBLTUR,
TWNBMAGE,
TWNBMARK,
TWNBMOAT,
TWNBRTUR,
TWNBSPEC,
TWNBSTAT,
TWNBTENT,
TWNBTHIE,
TWNBTVRN,
TWNBUP_1,
TWNBUP_3,
TWNBUP_4,
TWNBWEL2,
TWNBWELL,
TWNKBOAT,
TWNKCAPT,
TWNKCSTL,
TWNKDOCK,
TWNKDW_0,
TWNKDW_1,
TWNKDW_2,
TWNKDW_3,
TWNKDW_4,
TWNKDW_5,
TWNKEXT0,
TWNKEXT1,
TWNKEXT2,
TWNKLTUR,
TWNKMAGE,
TWNKMARK,
TWNKMOAT,
TWNKRTUR,
TWNKSPEC,
TWNKSTAT,
TWNKTENT,
TWNKTHIE,
TWNKTVRN,
TWNKUP_1,
TWNKUP_2,
TWNKUP_3,
TWNKUP_4,
TWNKUP_5,
TWNKWEL2,
TWNKWELL,
TWNNBOAT,
TWNNCAPT,
TWNNCSTL,
TWNNDOCK,
TWNNDW_0,
TWNNDW_1,
TWNNDW_2,
TWNNDW_3,
TWNNDW_4,
TWNNDW_5,
TWNNEXT0,
TWNNLTUR,
TWNNMAGE,
TWNNMARK,
TWNNMOAT,
TWNNRTUR,
TWNNSPEC,
TWNNSTAT,
TWNNTENT,
TWNNTHIE,
TWNNTVRN,
TWNNUP_1,
TWNNUP_2,
TWNNUP_3,
TWNNUP_4,
TWNNWEL2,
TWNNWELL,
TWNSBOAT,
TWNSCAPT,
TWNSCSTL,
TWNSDOCK,
TWNSDW_0,
TWNSDW_1,
TWNSDW_2,
TWNSDW_3,
TWNSDW_4,
TWNSDW_5,
TWNSEXT0,
TWNSEXT1,
TWNSLTUR,
TWNSMAGE,
TWNSMARK,
TWNSMOAT,
TWNSRTUR,
TWNSSPEC,
TWNSSTAT,
TWNSTENT,
TWNSTHIE,
TWNSTVRN,
TWNSUP_1,
TWNSUP_2,
TWNSUP_3,
TWNSWEL2,
TWNSWELL,
TWNWBOAT,
TWNWCAPT,
TWNWCSTL,
TWNWDOCK,
TWNWDW_0,
TWNWDW_1,
TWNWDW_2,
TWNWDW_3,
TWNWDW_4,
TWNWDW_5,
TWNWEXT0,
TWNWLTUR,
TWNWMAGE,
TWNWMARK,
TWNWMOAT,
TWNWRTUR,
TWNWSPEC,
TWNWSTAT,
TWNWTENT,
TWNWTHIE,
TWNWTVRN,
TWNWUP_3,
TWNWUP5B,
TWNWUP_5,
TWNWWEL2,
TWNWWELL,
TWNZBOAT,
TWNZCAPT,
TWNZCSTL,
TWNZDOCK,
TWNZDW_0,
TWNZDW_1,
TWNZDW_2,
TWNZDW_3,
TWNZDW_4,
TWNZDW_5,
TWNZEXT0,
TWNZLTUR,
TWNZMAGE,
TWNZMARK,
TWNZMOAT,
TWNZRTUR,
TWNZSPEC,
TWNZSTAT,
TWNZTENT,
TWNZTHIE,
TWNZTVRN,
TWNZUP_2,
TWNZUP_4,
TWNZUP_5,
TWNZWEL2,
TWNZWELL,
UNICORN,
VAMPIRE2,
VAMPIRE,
VGENBKGE,
VGENBKG,
VIEW_ALL,
VIEWARME,
VIEWARMY,
VIEWARSM,
VIEWDDOR,
VIEWGEN,
VIEWHROS,
VIEWMINE,
VIEWPUZL,
VIEWRSRC,
VIEWRTFX,
VIEWTWNS,
VIEWWRLD,
VWFLAG12,
VWFLAG4,
VWFLAG6,
WELEM,
WELLBKG,
WELLXTRA,
WINCMBBE,
WINCMBTB,
WINCMBT,
WINLOSEB,
WINLOSEE,
WINLOSE,
WOLF,
WRLK32,
WZRD32,
X_LOC1,
X_LOC2,
X_LOC3,
XPRIMARY,
Y_BFLG32,
Y_FLAG32,
YINYANG,
ZOMBIE2,
ZOMBIE,
// system
ROUTERED,
TELEPORT1,
TELEPORT2,
TELEPORT3,
FOUNTAIN,
TREASURE,
YELLOW_FONT,
YELLOW_SMALFONT,
UNKNOWN
};
class Header
{
public:
Header();
void Load(const u8* p);
u16 OffsetX(void) const{ return offset_x; }
u16 OffsetY(void) const{ return offset_y; }
u16 Width(void) const{ return width; }
u16 Height(void) const{ return height; }
u8 Type(void) const{ return type; }
u32 OffsetData(void) const{ return offset_data; }
inline static u8 SizeOf(void){ return 13; }
private:
u16 offset_x;
u16 offset_y;
u16 width;
u16 height;
u8 type;
u32 offset_data;
};
const char* GetString(const icn_t icn);
icn_t FromString(const char*);
u16 AnimationFrame(const icn_t icn, const u16 start, const u32 ticket = 0, const u8 quantity = 0);
bool RequiresAlpha(const icn_t icn);
bool isModifiedSprite(const icn_t icn);
bool NeedMinify4PocketPC(icn_t, u16);
bool SkipBottomForRedrawHeroes(icn_t, u16);
icn_t PORTxxxx(u8);
u8 GetMissIndex(icn_t, s16, s16);
bool isBattleMonsterICN(u16);
bool HighlyObjectSprite(icn_t, u16);
bool SkipRegistryFree(icn_t);
}
#endif

View File

@@ -1,485 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "spell.h"
#include "mp2.h"
#include "m82.h"
namespace M82
{
static const struct
{
m82_t type;
const char* string;
} m82map[] = {
{ AELMATTK, "AELMATTK.82M" },
{ AELMKILL, "AELMKILL.82M" },
{ AELMMOVE, "AELMMOVE.82M" },
{ AELMWNCE, "AELMWNCE.82M" },
{ ANTIMAGK, "ANTIMAGK.82M" },
{ ARCHATTK, "ARCHATTK.82M" },
{ ARCHKILL, "ARCHKILL.82M" },
{ ARCHMOVE, "ARCHMOVE.82M" },
{ ARCHSHOT, "ARCHSHOT.82M" },
{ ARCHWNCE, "ARCHWNCE.82M" },
{ ARMGEDN, "ARMGEDN.82M" },
{ BADLUCK, "BADLUCK.82M" },
{ BADMRLE, "BADMRLE.82M" },
{ BERZERK, "BERZERK.82M" },
{ BLESS, "BLESS.82M" },
{ BLIND, "BLIND.82M" },
{ BLOODLUS, "BLOODLUS.82M" },
{ BOARATTK, "BOARATTK.82M" },
{ BOARKILL, "BOARKILL.82M" },
{ BOARMOVE, "BOARMOVE.82M" },
{ BOARWNCE, "BOARWNCE.82M" },
{ BONEATTK, "BONEATTK.82M" },
{ BONEKILL, "BONEKILL.82M" },
{ BONEMOVE, "BONEMOVE.82M" },
{ BONEWNCE, "BONEWNCE.82M" },
{ BUILDTWN, "BUILDTWN.82M" },
{ CATSND00, "CATSND00.82M" },
{ CATSND02, "CATSND02.82M" },
{ CAVLATTK, "CAVLATTK.82M" },
{ CAVLKILL, "CAVLKILL.82M" },
{ CAVLMOVE, "CAVLMOVE.82M" },
{ CAVLWNCE, "CAVLWNCE.82M" },
{ CHAINLTE, "CHAINLTE.82M" },
{ CNTRATTK, "CNTRATTK.82M" },
{ CNTRKILL, "CNTRKILL.82M" },
{ CNTRMOVE, "CNTRMOVE.82M" },
{ CNTRSHOT, "CNTRSHOT.82M" },
{ CNTRWNCE, "CNTRWNCE.82M" },
{ COLDRAY, "COLDRAY.82M" },
{ COLDRING, "COLDRING.82M" },
{ CURE, "CURE.82M" },
{ CURSE, "CURSE.82M" },
{ CYCLATTK, "CYCLATTK.82M" },
{ CYCLKILL, "CYCLKILL.82M" },
{ CYCLMOVE, "CYCLMOVE.82M" },
{ CYCLWNCE, "CYCLWNCE.82M" },
{ DIGSOUND, "DIGSOUND.82M" },
{ DIPMAGK, "DIPMAGK.82M" },
{ DISRUPTR, "DISRUPTR.82M" },
{ DRAWBRG, "DRAWBRG.82M" },
{ DRGNATTK, "DRGNATTK.82M" },
{ DRGNKILL, "DRGNKILL.82M" },
{ DRGNMOVE, "DRGNMOVE.82M" },
{ DRGNSLAY, "DRGNSLAY.82M" },
{ DRGNWNCE, "DRGNWNCE.82M" },
{ DRUIATTK, "DRUIATTK.82M" },
{ DRUIKILL, "DRUIKILL.82M" },
{ DRUIMOVE, "DRUIMOVE.82M" },
{ DRUISHOT, "DRUISHOT.82M" },
{ DRUIWNCE, "DRUIWNCE.82M" },
{ DWRFATTK, "DWRFATTK.82M" },
{ DWRFKILL, "DWRFKILL.82M" },
{ DWRFMOVE, "DWRFMOVE.82M" },
{ DWRFWNCE, "DWRFWNCE.82M" },
{ EELMATTK, "EELMATTK.82M" },
{ EELMKILL, "EELMKILL.82M" },
{ EELMMOVE, "EELMMOVE.82M" },
{ EELMWNCE, "EELMWNCE.82M" },
{ ELF_ATTK, "ELF_ATTK.82M" },
{ ELF_KILL, "ELF_KILL.82M" },
{ ELF_MOVE, "ELF_MOVE.82M" },
{ ELF_SHOT, "ELF_SHOT.82M" },
{ ELF_WNCE, "ELF_WNCE.82M" },
{ ERTHQUAK, "ERTHQUAK.82M" },
{ EXPERNCE, "EXPERNCE.82M" },
{ FELMATTK, "FELMATTK.82M" },
{ FELMKILL, "FELMKILL.82M" },
{ FELMMOVE, "FELMMOVE.82M" },
{ FELMWNCE, "FELMWNCE.82M" },
{ FIREBALL, "FIREBALL.82M" },
{ GARGATTK, "GARGATTK.82M" },
{ GARGKILL, "GARGKILL.82M" },
{ GARGMOVE, "GARGMOVE.82M" },
{ GARGWNCE, "GARGWNCE.82M" },
{ GBLNATTK, "GBLNATTK.82M" },
{ GBLNKILL, "GBLNKILL.82M" },
{ GBLNMOVE, "GBLNMOVE.82M" },
{ GBLNWNCE, "GBLNWNCE.82M" },
{ GENIATTK, "GENIATTK.82M" },
{ GENIKILL, "GENIKILL.82M" },
{ GENIMOVE, "GENIMOVE.82M" },
{ GENIWNCE, "GENIWNCE.82M" },
{ GHSTATTK, "GHSTATTK.82M" },
{ GHSTKILL, "GHSTKILL.82M" },
{ GHSTMOVE, "GHSTMOVE.82M" },
{ GHSTWNCE, "GHSTWNCE.82M" },
{ GOLMATTK, "GOLMATTK.82M" },
{ GOLMKILL, "GOLMKILL.82M" },
{ GOLMMOVE, "GOLMMOVE.82M" },
{ GOLMWNCE, "GOLMWNCE.82M" },
{ GOODLUCK, "GOODLUCK.82M" },
{ GOODMRLE, "GOODMRLE.82M" },
{ GRIFATTK, "GRIFATTK.82M" },
{ GRIFKILL, "GRIFKILL.82M" },
{ GRIFMOVE, "GRIFMOVE.82M" },
{ GRIFWNCE, "GRIFWNCE.82M" },
{ H2MINE, "H2MINE.82M" },
{ HALFATTK, "HALFATTK.82M" },
{ HALFKILL, "HALFKILL.82M" },
{ HALFMOVE, "HALFMOVE.82M" },
{ HALFSHOT, "HALFSHOT.82M" },
{ HALFWNCE, "HALFWNCE.82M" },
{ HASTE, "HASTE.82M" },
{ HYDRATTK, "HYDRATTK.82M" },
{ HYDRKILL, "HYDRKILL.82M" },
{ HYDRMOVE, "HYDRMOVE.82M" },
{ HYDRWNCE, "HYDRWNCE.82M" },
{ HYPNOTIZ, "HYPNOTIZ.82M" },
{ KEEPSHOT, "KEEPSHOT.82M" },
{ KILLFADE, "KILLFADE.82M" },
{ LICHATTK, "LICHATTK.82M" },
{ LICHEXPL, "LICHEXPL.82M" },
{ LICHKILL, "LICHKILL.82M" },
{ LICHMOVE, "LICHMOVE.82M" },
{ LICHSHOT, "LICHSHOT.82M" },
{ LICHWNCE, "LICHWNCE.82M" },
{ LIGHTBLT, "LIGHTBLT.82M" },
{ LOOP0000, "LOOP0000.82M" },
{ LOOP0001, "LOOP0001.82M" },
{ LOOP0002, "LOOP0002.82M" },
{ LOOP0003, "LOOP0003.82M" },
{ LOOP0004, "LOOP0004.82M" },
{ LOOP0005, "LOOP0005.82M" },
{ LOOP0006, "LOOP0006.82M" },
{ LOOP0007, "LOOP0007.82M" },
{ LOOP0008, "LOOP0008.82M" },
{ LOOP0009, "LOOP0009.82M" },
{ LOOP0010, "LOOP0010.82M" },
{ LOOP0011, "LOOP0011.82M" },
{ LOOP0012, "LOOP0012.82M" },
{ LOOP0013, "LOOP0013.82M" },
{ LOOP0014, "LOOP0014.82M" },
{ LOOP0015, "LOOP0015.82M" },
{ LOOP0016, "LOOP0016.82M" },
{ LOOP0017, "LOOP0017.82M" },
{ LOOP0018, "LOOP0018.82M" },
{ LOOP0019, "LOOP0019.82M" },
{ LOOP0020, "LOOP0020.82M" },
{ LOOP0021, "LOOP0021.82M" },
{ LOOP0022, "LOOP0022.82M" },
{ LOOP0023, "LOOP0023.82M" },
{ LOOP0024, "LOOP0024.82M" },
{ LOOP0025, "LOOP0025.82M" },
{ LOOP0026, "LOOP0026.82M" },
{ LOOP0027, "LOOP0027.82M" },
{ MAGCAROW, "MAGCAROW.82M" },
{ MAGEATTK, "MAGEATTK.82M" },
{ MAGEKILL, "MAGEKILL.82M" },
{ MAGEMOVE, "MAGEMOVE.82M" },
{ MAGESHOT, "MAGESHOT.82M" },
{ MAGEWNCE, "MAGEWNCE.82M" },
{ MASSBLES, "MASSBLES.82M" },
{ MASSCURE, "MASSCURE.82M" },
{ MASSCURS, "MASSCURS.82M" },
{ MASSHAST, "MASSHAST.82M" },
{ MASSSHIE, "MASSSHIE.82M" },
{ MASSSLOW, "MASSSLOW.82M" },
{ MEDSATTK, "MEDSATTK.82M" },
{ MEDSKILL, "MEDSKILL.82M" },
{ MEDSMOVE, "MEDSMOVE.82M" },
{ MEDSWNCE, "MEDSWNCE.82M" },
{ METEOR, "METEOR~1.82M" },
{ MINOATTK, "MINOATTK.82M" },
{ MINOKILL, "MINOKILL.82M" },
{ MINOMOVE, "MINOMOVE.82M" },
{ MINOWNCE, "MINOWNCE.82M" },
{ MIRRORIM, "MIRRORIM.82M" },
{ MNRDEATH, "MNRDEATH.82M" },
{ MUMYATTK, "MUMYATTK.82M" },
{ MUMYKILL, "MUMYKILL.82M" },
{ MUMYMOVE, "MUMYMOVE.82M" },
{ MUMYWNCE, "MUMYWNCE.82M" },
{ NMADATTK, "NMADATTK.82M" },
{ NMADKILL, "NMADKILL.82M" },
{ NMADMOVE, "NMADMOVE.82M" },
{ NMADWNCE, "NMADWNCE.82M" },
{ NWHEROLV, "NWHEROLV.82M" },
{ OGREATTK, "OGREATTK.82M" },
{ OGREKILL, "OGREKILL.82M" },
{ OGREMOVE, "OGREMOVE.82M" },
{ OGREWNCE, "OGREWNCE.82M" },
{ ORC_ATTK, "ORC_ATTK.82M" },
{ ORC_KILL, "ORC_KILL.82M" },
{ ORC_MOVE, "ORC_MOVE.82M" },
{ ORC_SHOT, "ORC_SHOT.82M" },
{ ORC_WNCE, "ORC_WNCE.82M" },
{ PARALIZE, "PARALIZE.82M" },
{ PHOEATTK, "PHOEATTK.82M" },
{ PHOEKILL, "PHOEKILL.82M" },
{ PHOEMOVE, "PHOEMOVE.82M" },
{ PHOEWNCE, "PHOEWNCE.82M" },
{ PICKUP01, "PICKUP01.82M" },
{ PICKUP02, "PICKUP02.82M" },
{ PICKUP03, "PICKUP03.82M" },
{ PICKUP04, "PICKUP04.82M" },
{ PICKUP05, "PICKUP05.82M" },
{ PICKUP06, "PICKUP06.82M" },
{ PICKUP07, "PICKUP07.82M" },
{ PIKEATTK, "PIKEATTK.82M" },
{ PIKEKILL, "PIKEKILL.82M" },
{ PIKEMOVE, "PIKEMOVE.82M" },
{ PIKEWNCE, "PIKEWNCE.82M" },
{ PLDNATTK, "PLDNATTK.82M" },
{ PLDNKILL, "PLDNKILL.82M" },
{ PLDNMOVE, "PLDNMOVE.82M" },
{ PLDNWNCE, "PLDNWNCE.82M" },
{ PREBATTL, "PREBATTL.82M" },
{ PROTECT, "PROTECT.82M" },
{ PSNTATTK, "PSNTATTK.82M" },
{ PSNTKILL, "PSNTKILL.82M" },
{ PSNTMOVE, "PSNTMOVE.82M" },
{ PSNTWNCE, "PSNTWNCE.82M" },
{ RESURECT, "RESURECT.82M" },
{ RESURTRU, "RESURTRU.82M" },
{ ROC_ATTK, "ROC_ATTK.82M" },
{ ROC_KILL, "ROC_KILL.82M" },
{ ROC_MOVE, "ROC_MOVE.82M" },
{ ROC_WNCE, "ROC_WNCE.82M" },
{ ROGUATTK, "ROGUATTK.82M" },
{ ROGUKILL, "ROGUKILL.82M" },
{ ROGUMOVE, "ROGUMOVE.82M" },
{ ROGUWNCE, "ROGUWNCE.82M" },
{ RSBRYFZL, "RSBRYFZL.82M" },
{ SHIELD, "SHIELD.82M" },
{ SKELATTK, "SKELATTK.82M" },
{ SKELKILL, "SKELKILL.82M" },
{ SKELMOVE, "SKELMOVE.82M" },
{ SKELWNCE, "SKELWNCE.82M" },
{ SLOW, "SLOW.82M" },
{ SPRTATTK, "SPRTATTK.82M" },
{ SPRTKILL, "SPRTKILL.82M" },
{ SPRTMOVE, "SPRTMOVE.82M" },
{ SPRTWNCE, "SPRTWNCE.82M" },
{ STELSKIN, "STELSKIN.82M" },
{ STONESKI, "STONESKI.82M" },
{ STONSKIN, "STONSKIN.82M" },
{ STORM, "STORM.82M" },
{ SUMNELM, "SUMNELM.82M" },
{ SWDMATTK, "SWDMATTK.82M" },
{ SWDMKILL, "SWDMKILL.82M" },
{ SWDMMOVE, "SWDMMOVE.82M" },
{ SWDMWNCE, "SWDMWNCE.82M" },
{ TELEIN, "TELEIN.82M" },
{ TELPTIN, "TELPTIN.82M" },
{ TELPTOUT, "TELPTOUT.82M" },
{ TITNATTK, "TITNATTK.82M" },
{ TITNKILL, "TITNKILL.82M" },
{ TITNMOVE, "TITNMOVE.82M" },
{ TITNSHOT, "TITNSHOT.82M" },
{ TITNWNCE, "TITNWNCE.82M" },
{ TREASURE, "TREASURE.82M" },
{ TRLLATTK, "TRLLATTK.82M" },
{ TRLLKILL, "TRLLKILL.82M" },
{ TRLLMOVE, "TRLLMOVE.82M" },
{ TRLLSHOT, "TRLLSHOT.82M" },
{ TRLLWNCE, "TRLLWNCE.82M" },
{ UNICATTK, "UNICATTK.82M" },
{ UNICKILL, "UNICKILL.82M" },
{ UNICMOVE, "UNICMOVE.82M" },
{ UNICWNCE, "UNICWNCE.82M" },
{ VAMPATTK, "VAMPATTK.82M" },
{ VAMPEXT1, "VAMPEXT1.82M" },
{ VAMPEXT2, "VAMPEXT2.82M" },
{ VAMPKILL, "VAMPKILL.82M" },
{ VAMPMOVE, "VAMPMOVE.82M" },
{ VAMPWNCE, "VAMPWNCE.82M" },
{ WELMATTK, "WELMATTK.82M" },
{ WELMKILL, "WELMKILL.82M" },
{ WELMMOVE, "WELMMOVE.82M" },
{ WELMWNCE, "WELMWNCE.82M" },
{ WOLFATTK, "WOLFATTK.82M" },
{ WOLFKILL, "WOLFKILL.82M" },
{ WOLFMOVE, "WOLFMOVE.82M" },
{ WOLFWNCE, "WOLFWNCE.82M" },
{ WSND00, "WSND00.82M" },
{ WSND01, "WSND01.82M" },
{ WSND02, "WSND02.82M" },
{ WSND03, "WSND03.82M" },
{ WSND04, "WSND04.82M" },
{ WSND05, "WSND05.82M" },
{ WSND06, "WSND06.82M" },
{ WSND10, "WSND10.82M" },
{ WSND11, "WSND11.82M" },
{ WSND12, "WSND12.82M" },
{ WSND13, "WSND13.82M" },
{ WSND14, "WSND14.82M" },
{ WSND15, "WSND15.82M" },
{ WSND16, "WSND16.82M" },
{ WSND20, "WSND20.82M" },
{ WSND21, "WSND21.82M" },
{ WSND22, "WSND22.82M" },
{ WSND23, "WSND23.82M" },
{ WSND24, "WSND24.82M" },
{ WSND25, "WSND25.82M" },
{ WSND26, "WSND26.82M" },
{ ZOMBATTK, "ZOMBATTK.82M" },
{ ZOMBKILL, "ZOMBKILL.82M" },
{ ZOMBMOVE, "ZOMBMOVE.82M" },
{ ZOMBWNCE, "ZOMBWNCE.82M" },
};
}
const char* M82::GetString(const m82_t m82)
{
return m82map[m82].string;
}
M82::m82_t M82::FromSpell(u8 spell)
{
switch(spell)
{
case Spell::FIREBALL: return FIREBALL;
case Spell::FIREBLAST: return FIREBALL;
case Spell::LIGHTNINGBOLT: return LIGHTBLT;
case Spell::CHAINLIGHTNING: return CHAINLTE;
case Spell::TELEPORT: return TELEIN;
case Spell::CURE: return CURE;
case Spell::MASSCURE: return MASSCURE;
case Spell::RESURRECT: return RESURECT;
case Spell::RESURRECTTRUE: return RESURTRU;
case Spell::HASTE: return HASTE;
case Spell::MASSHASTE: return MASSHAST;
case Spell::SLOW: return SLOW;
case Spell::MASSSLOW: return MASSSLOW;
case Spell::BLIND: return BLIND;
case Spell::BLESS: return BLESS;
case Spell::MASSBLESS: return MASSBLES;
case Spell::STONESKIN: return STONSKIN;
case Spell::STEELSKIN: return STELSKIN;
case Spell::CURSE: return CURSE;
case Spell::MASSCURSE: return MASSCURS;
case Spell::ANTIMAGIC: return ANTIMAGK;
case Spell::DISPEL: return DIPMAGK;
case Spell::MASSDISPEL: return DIPMAGK;
case Spell::ARROW: return MAGCAROW;
case Spell::BERSERKER: return BERZERK;
case Spell::ARMAGEDDON: return ARMGEDN;
case Spell::ELEMENTALSTORM: return STORM;
case Spell::METEORSHOWER: return METEOR;
case Spell::PARALYZE: return PARALIZE;
case Spell::HYPNOTIZE: return HYPNOTIZ;
case Spell::COLDRAY: return COLDRAY;
case Spell::COLDRING: return COLDRING;
case Spell::DISRUPTINGRAY: return DISRUPTR;
case Spell::DEATHRIPPLE: return MNRDEATH;
case Spell::DRAGONSLAYER: return DRGNSLAY;
case Spell::BLOODLUST: return BLOODLUS;
case Spell::ANIMATEDEAD: return RESURECT;
case Spell::MIRRORIMAGE: return MIRRORIM;
case Spell::SHIELD: return SHIELD;
case Spell::MASSSHIELD: return MASSSHIE;
case Spell::SUMMONEELEMENT: return SUMNELM;
case Spell::SUMMONAELEMENT: return SUMNELM;
case Spell::SUMMONFELEMENT: return SUMNELM;
case Spell::SUMMONWELEMENT: return SUMNELM;
case Spell::EARTHQUAKE: return ERTHQUAK;
case Spell::HAUNT: return H2MINE;
case Spell::STONE: return PARALIZE;
default: break;
}
return UNKNOWN;
}
u8 M82::GetIndexLOOP00XXFromObject(u8 obj)
{
switch(obj)
{
case MP2::OBJ_BUOY: return 0;
case MP2::OBJ_SHIPWRECK:
case MP2::OBJ_DERELICTSHIP: return 1;
case MP2::OBJ_COAST: return 2;
case MP2::OBJ_ORACLE: return 3;
case MP2::OBJ_STONELIGHTS: return 4;
case MP2::OBJ_LAVAPOOL: return 5;
case MP2::OBJ_ALCHEMYLAB: return 6;
case MP2::OBJ_WATERWHEEL: return 9;
case MP2::OBJ_CAMPFIRE: return 10;
case MP2::OBJ_WINDMILL: return 11;
case MP2::OBJ_ARTESIANSPRING:
case MP2::OBJ_FOUNTAIN: return 12;
case MP2::OBJ_WATERLAKE:
case MP2::OBJ_WATERINGHOLE: return 13;
case MP2::OBJ_MINES: return 15;
case MP2::OBJ_SAWMILL: return 16;
case MP2::OBJ_DAEMONCAVE: return 17;
case MP2::OBJ_SHRINE1:
case MP2::OBJ_SHRINE2:
case MP2::OBJ_SHRINE3: return 18;
case MP2::OBJ_TARPIT: return 21;
case MP2::OBJ_TRADINGPOST: return 22;
case MP2::OBJ_RUINS: return 24;
case MP2::OBJ_PEASANTHUT:
case MP2::OBJ_DWARFCOTT:
case MP2::OBJ_ARCHERHOUSE: return 25;
case MP2::OBJ_VOLCANO: return 27;
default: break;
}
return 0xFF;
}
M82::m82_t M82::GetLOOP00XX(u8 index)
{
switch(index)
{
case 0: return LOOP0000;
case 1: return LOOP0001;
case 2: return LOOP0002;
case 3: return LOOP0003;
case 4: return LOOP0004;
case 5: return LOOP0005;
case 6: return LOOP0006;
case 7: return LOOP0007;
case 8: return LOOP0008;
case 9: return LOOP0009;
case 10: return LOOP0010;
case 11: return LOOP0011;
case 12: return LOOP0012;
case 13: return LOOP0013;
case 14: return LOOP0014;
case 15: return LOOP0015;
case 16: return LOOP0016;
case 17: return LOOP0017;
case 18: return LOOP0018;
case 19: return LOOP0019;
case 20: return LOOP0020;
case 21: return LOOP0021;
case 22: return LOOP0022;
case 23: return LOOP0023;
case 24: return LOOP0024;
case 25: return LOOP0025;
case 26: return LOOP0026;
case 27: return LOOP0027;
default: break;
}
return UNKNOWN;
}

View File

@@ -1,354 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2M82_H
#define H2M82_H
#include "gamedefs.h"
#define LOOPXX_COUNT 28
namespace M82
{
enum m82_t
{
AELMATTK,
AELMKILL,
AELMMOVE,
AELMWNCE,
ANTIMAGK,
ARCHATTK,
ARCHKILL,
ARCHMOVE,
ARCHSHOT,
ARCHWNCE,
ARMGEDN,
BADLUCK,
BADMRLE,
BERZERK,
BLESS,
BLIND,
BLOODLUS,
BOARATTK,
BOARKILL,
BOARMOVE,
BOARWNCE,
BONEATTK,
BONEKILL,
BONEMOVE,
BONEWNCE,
BUILDTWN,
CATSND00,
CATSND02,
CAVLATTK,
CAVLKILL,
CAVLMOVE,
CAVLWNCE,
CHAINLTE,
CNTRATTK,
CNTRKILL,
CNTRMOVE,
CNTRSHOT,
CNTRWNCE,
COLDRAY,
COLDRING,
CURE,
CURSE,
CYCLATTK,
CYCLKILL,
CYCLMOVE,
CYCLWNCE,
DIGSOUND,
DIPMAGK,
DISRUPTR,
DRAWBRG,
DRGNATTK,
DRGNKILL,
DRGNMOVE,
DRGNSLAY,
DRGNWNCE,
DRUIATTK,
DRUIKILL,
DRUIMOVE,
DRUISHOT,
DRUIWNCE,
DWRFATTK,
DWRFKILL,
DWRFMOVE,
DWRFWNCE,
EELMATTK,
EELMKILL,
EELMMOVE,
EELMWNCE,
ELF_ATTK,
ELF_KILL,
ELF_MOVE,
ELF_SHOT,
ELF_WNCE,
ERTHQUAK,
EXPERNCE,
FELMATTK,
FELMKILL,
FELMMOVE,
FELMWNCE,
FIREBALL,
GARGATTK,
GARGKILL,
GARGMOVE,
GARGWNCE,
GBLNATTK,
GBLNKILL,
GBLNMOVE,
GBLNWNCE,
GENIATTK,
GENIKILL,
GENIMOVE,
GENIWNCE,
GHSTATTK,
GHSTKILL,
GHSTMOVE,
GHSTWNCE,
GOLMATTK,
GOLMKILL,
GOLMMOVE,
GOLMWNCE,
GOODLUCK,
GOODMRLE,
GRIFATTK,
GRIFKILL,
GRIFMOVE,
GRIFWNCE,
H2MINE,
HALFATTK,
HALFKILL,
HALFMOVE,
HALFSHOT,
HALFWNCE,
HASTE,
HYDRATTK,
HYDRKILL,
HYDRMOVE,
HYDRWNCE,
HYPNOTIZ,
KEEPSHOT,
KILLFADE,
LICHATTK,
LICHEXPL,
LICHKILL,
LICHMOVE,
LICHSHOT,
LICHWNCE,
LIGHTBLT,
LOOP0000,
LOOP0001,
LOOP0002,
LOOP0003,
LOOP0004,
LOOP0005,
LOOP0006,
LOOP0007,
LOOP0008,
LOOP0009,
LOOP0010,
LOOP0011,
LOOP0012,
LOOP0013,
LOOP0014,
LOOP0015,
LOOP0016,
LOOP0017,
LOOP0018,
LOOP0019,
LOOP0020,
LOOP0021,
LOOP0022,
LOOP0023,
LOOP0024,
LOOP0025,
LOOP0026,
LOOP0027,
MAGCAROW,
MAGEATTK,
MAGEKILL,
MAGEMOVE,
MAGESHOT,
MAGEWNCE,
MASSBLES,
MASSCURE,
MASSCURS,
MASSHAST,
MASSSHIE,
MASSSLOW,
MEDSATTK,
MEDSKILL,
MEDSMOVE,
MEDSWNCE,
METEOR,
MINOATTK,
MINOKILL,
MINOMOVE,
MINOWNCE,
MIRRORIM,
MNRDEATH,
MUMYATTK,
MUMYKILL,
MUMYMOVE,
MUMYWNCE,
NMADATTK,
NMADKILL,
NMADMOVE,
NMADWNCE,
NWHEROLV,
OGREATTK,
OGREKILL,
OGREMOVE,
OGREWNCE,
ORC_ATTK,
ORC_KILL,
ORC_MOVE,
ORC_SHOT,
ORC_WNCE,
PARALIZE,
PHOEATTK,
PHOEKILL,
PHOEMOVE,
PHOEWNCE,
PICKUP01,
PICKUP02,
PICKUP03,
PICKUP04,
PICKUP05,
PICKUP06,
PICKUP07,
PIKEATTK,
PIKEKILL,
PIKEMOVE,
PIKEWNCE,
PLDNATTK,
PLDNKILL,
PLDNMOVE,
PLDNWNCE,
PREBATTL,
PROTECT,
PSNTATTK,
PSNTKILL,
PSNTMOVE,
PSNTWNCE,
RESURECT,
RESURTRU,
ROC_ATTK,
ROC_KILL,
ROC_MOVE,
ROC_WNCE,
ROGUATTK,
ROGUKILL,
ROGUMOVE,
ROGUWNCE,
RSBRYFZL,
SHIELD,
SKELATTK,
SKELKILL,
SKELMOVE,
SKELWNCE,
SLOW,
SPRTATTK,
SPRTKILL,
SPRTMOVE,
SPRTWNCE,
STELSKIN,
STONESKI,
STONSKIN,
STORM,
SUMNELM,
SWDMATTK,
SWDMKILL,
SWDMMOVE,
SWDMWNCE,
TELEIN,
TELPTIN,
TELPTOUT,
TITNATTK,
TITNKILL,
TITNMOVE,
TITNSHOT,
TITNWNCE,
TREASURE,
TRLLATTK,
TRLLKILL,
TRLLMOVE,
TRLLSHOT,
TRLLWNCE,
UNICATTK,
UNICKILL,
UNICMOVE,
UNICWNCE,
VAMPATTK,
VAMPEXT1,
VAMPEXT2,
VAMPKILL,
VAMPMOVE,
VAMPWNCE,
WELMATTK,
WELMKILL,
WELMMOVE,
WELMWNCE,
WOLFATTK,
WOLFKILL,
WOLFMOVE,
WOLFWNCE,
WSND00,
WSND01,
WSND02,
WSND03,
WSND04,
WSND05,
WSND06,
WSND10,
WSND11,
WSND12,
WSND13,
WSND14,
WSND15,
WSND16,
WSND20,
WSND21,
WSND22,
WSND23,
WSND24,
WSND25,
WSND26,
ZOMBATTK,
ZOMBKILL,
ZOMBMOVE,
ZOMBWNCE,
UNKNOWN
};
const char* GetString(const m82_t m82);
m82_t FromSpell(u8);
u8 GetIndexLOOP00XXFromObject(u8);
m82_t GetLOOP00XX(u8);
}
#endif

View File

@@ -1,187 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Josh Matthews <josh@joshmatthews.net> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <string>
#include <sstream>
#include <iomanip>
#include "race.h"
#include "ground.h"
#include "mus.h"
#include "settings.h"
#include "mp2.h"
namespace MUS
{
static const struct
{
mus_t type;
const char* string;
} musmap[] = {
{ UNUSED, "" },
{ DATATRACK, "" },
{ BATTLE1, "Battle (1)" },
{ BATTLE2, "Battle (2)" },
{ BATTLE3, "Battle (3)" },
{ BARBARIAN, "Barbarian Castle" },
{ SORCERESS, "Sorceress Castle" },
{ WARLOCK, "Warlock Castle" },
{ WIZARD, "Wizard Castle" },
{ NECROMANCER, "Necromancer Castle" },
{ KNIGHT, "Knight Castle" },
{ LAVA, "Lava Theme" },
{ WASTELAND, "Wasteland Theme" },
{ DESERT, "Desert Theme" },
{ SNOW, "Snow Theme" },
{ SWAMP, "Swamp Theme" },
{ BEACH, "Ocean Theme" },
{ DIRT, "Dirt Theme" },
{ GRASS, "Grass Theme" },
{ LOSTGAME, "Lost Game" },
{ WEEK1, "Week (1)" },
{ WEEK2_MONTH1, "Week (2) Month (1)" },
{ MONTH2, "Month (2)" },
{ PUZZLE, "Map Puzzle" },
{ ROLAND, "Roland's Campaign" },
{ CARAVANS, "25" },
{ CARAVANS_2, "26" },
{ CARAVANS_3, "27" },
{ COMPUTER, "28" },
{ BATTLEWIN, "29" },
{ BATTLELOSE, "30" },
{ DEATH, "31" },
{ WATERSPRING, "32" },
{ ARABIAN, "33" },
{ NOMADTENTS, "34" },
{ TREEHOUSE, "35" },
{ DEMONCAVE, "36" },
{ EXPERIENCE, "37" },
{ SKILL, "38" },
{ WATCHTOWER, "39" },
{ EVENT15, "40" },
{ NEWS, "41" },
{ MAINMENU, "Main Menu" },
{ VICTORY, "Scenario Victory" },
{ UNKNOWN, "???" }
};
const std::string GetString(const mus_t mus, bool shortname)
{
std::stringstream sstream;
sstream << std::setw(2) << std::setfill('0') << (int)mus;
if(shortname)
sstream << ".ogg";
else
sstream << " " << musmap[mus].string << ".ogg";
return sstream.str();
}
}
MUS::mus_t MUS::FromGround(const u16 ground)
{
switch(ground)
{
case Maps::Ground::DESERT: return DESERT;
case Maps::Ground::SNOW: return SNOW;
case Maps::Ground::SWAMP: return SWAMP;
case Maps::Ground::WASTELAND: return WASTELAND;
case Maps::Ground::BEACH: return BEACH;
case Maps::Ground::LAVA: return LAVA;
case Maps::Ground::DIRT: return DIRT;
case Maps::Ground::GRASS: return GRASS;
default: break;
}
return UNKNOWN;
}
MUS::mus_t MUS::FromRace(const u8 race)
{
switch(race)
{
case Race::KNGT: return KNIGHT;
case Race::BARB: return BARBARIAN;
case Race::SORC: return SORCERESS;
case Race::WRLK: return WARLOCK;
case Race::WZRD: return WIZARD;
case Race::NECR: return NECROMANCER;
default: break;
}
return UNKNOWN;
}
MUS::mus_t MUS::FromMapObject(u8 object)
{
if(!Settings::Get().CDMusic())
return MUS::UNKNOWN;
switch(object)
{
case MP2::OBJ_WITCHSHUT:
case MP2::OBJ_FORT:
case MP2::OBJ_MERCENARYCAMP:
case MP2::OBJ_DOCTORHUT:
case MP2::OBJ_STANDINGSTONES:
return MUS::SKILL;
case MP2::OBJ_GAZEBO:
case MP2::OBJ_TREEKNOWLEDGE:
return MUS::EXPERIENCE;
case MP2::OBJ_DAEMONCAVE:
return MUS::DEMONCAVE;
case MP2::OBJ_TREEHOUSE:
case MP2::OBJ_TREECITY:
return MUS::TREEHOUSE;
case MP2::OBJ_WATCHTOWER:
return MUS::WATCHTOWER;
case MP2::OBJ_DESERTTENT:
return MUS::NOMADTENTS;
case MP2::OBJ_ARTESIANSPRING:
return MUS::WATERSPRING;
case MP2::OBJ_SPHINX:
return MUS::ARABIAN;
case MP2::OBJ_EVENT:
return MUS::NEWS;
default:
return MUS::UNKNOWN;
}
}
MUS::mus_t MUS::GetBattleRandom(void)
{
switch(Rand::Get(1, 3))
{
case 1: return BATTLE1;
case 2: return BATTLE2;
case 3: return BATTLE3;
default: break;
}
return UNKNOWN;
}

View File

@@ -1,89 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Josh Matthews <josh@joshmatthews.net> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2MUS_H
#define H2MUS_H
#include "gamedefs.h"
namespace MUS
{
enum mus_t
{
UNUSED,
DATATRACK,
BATTLE1,
BATTLE2,
BATTLE3,
BARBARIAN,
SORCERESS,
WARLOCK,
WIZARD,
NECROMANCER,
KNIGHT,
LAVA,
WASTELAND,
DESERT,
SNOW,
SWAMP,
BEACH,
DIRT,
GRASS,
LOSTGAME,
WEEK1,
WEEK2_MONTH1,
MONTH2,
PUZZLE,
ROLAND,
CARAVANS,
CARAVANS_2,
CARAVANS_3,
COMPUTER,
BATTLEWIN,
BATTLELOSE,
DEATH,
WATERSPRING,
ARABIAN,
NOMADTENTS,
TREEHOUSE,
DEMONCAVE,
EXPERIENCE,
SKILL,
WATCHTOWER,
EVENT15,
NEWS,
MAINMENU,
VICTORY,
UNKNOWN
};
const std::string GetString(const mus_t mus, bool shortname = false);
mus_t FromGround(const u16 ground);
mus_t FromRace(const u8 race);
mus_t FromMapObject(u8 object);
mus_t GetBattleRandom(void);
}
#endif

View File

@@ -1,189 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "settings.h"
#include "icn.h"
#include "cursor.h"
#include "display.h"
#include "sprite.h"
Sprite::Sprite() : offsetX(0), offsetY(0)
{
}
void Sprite::SetOffset(s16 ox, s16 oy)
{
offsetX = ox;
offsetY = oy;
}
void Sprite::DrawICN(Surface & sf, const u8* cur, const u32 size, bool reflect)
{
if(NULL == cur || 0 == size) return;
const u8 *max = cur + size;
u8 c = 0;
u16 x = reflect ? sf.w() - 1 : 0;
u16 y = 0;
const u32 shadow = sf.isAlpha() ? sf.MapRGB(0, 0, 0, 0x40) : sf.GetColorKey();
// lock surface
sf.Lock();
while(1)
{
// 0x00 - end line
if(0 == *cur)
{
++y;
x = reflect ? sf.w() - 1 : 0;
++cur;
}
else
// 0x7F - count data
if(0x80 > *cur)
{
c = *cur;
++cur;
while(c-- && cur < max)
{
sf.SetPixel(x, y, sf.GetColor(*cur));
reflect ? x-- : x++;
++cur;
}
}
else
// 0x80 - end data
if(0x80 == *cur)
{
break;
}
else
// 0xBF - skip data
if(0xC0 > *cur)
{
reflect ? x -= *cur - 0x80 : x += *cur - 0x80;
++cur;
}
else
// 0xC0 - shadow
if(0xC0 == *cur)
{
++cur;
c = *cur % 4 ? *cur % 4 : *(++cur);
while(c--){ if(sf.isAlpha()) sf.SetPixel(x, y, shadow); reflect ? x-- : x++; }
++cur;
}
else
// 0xC1
if(0xC1 == *cur)
{
++cur;
c = *cur;
++cur;
while(c--){ sf.SetPixel(x, y, sf.GetColor(*cur)); reflect ? x-- : x++; }
++cur;
}
else
{
c = *cur - 0xC0;
++cur;
while(c--){ sf.SetPixel(x, y, sf.GetColor(*cur)); reflect ? x-- : x++; }
++cur;
}
if(cur >= max)
{
DEBUG(DBG_ENGINE , DBG_WARN, "Sprite: index out of range");
break;
}
}
// unlock surface
sf.Unlock();
}
u32 Sprite::GetSize(void) const
{
return Surface::GetSize() + sizeof(offsetX) + sizeof(offsetY);
}
void Sprite::ScaleMinifyByTwo(void)
{
Cursor & cursor = Cursor::Get();
Display & display = Display::Get();
if(w() > 3 && h() > 3)
{
u16 theme = 0;
if(cursor.isVisible() && Cursor::WAIT != cursor.Themes())
{
theme = cursor.Themes();
cursor.SetThemes(Cursor::WAIT);
cursor.Show();
display.Flip();
}
Surface sf;
Surface::ScaleMinifyByTwo(sf, *this, cursor.isVisible());
Surface::Swap(sf, *this);
if(theme)
{
cursor.SetThemes(theme);
cursor.Show();
display.Flip();
}
}
offsetX /= 2;
offsetY /= 2;
}
void Sprite::AddonExtensionModify(Sprite & sp, u16 icn, u16 index)
{
switch(icn)
{
case ICN::AELEM:
if(sp.w() > 3 && sp.h() > 3)
{
Surface sf;
Surface::MakeContour(sf, sp, sp.GetColor(0xEF));
sp.Blit(sf, -1, -1);
}
break;
default: break;
}
}
void Sprite::BlitSpriteWithAlpha(Surface & dst, u8 alpha, s16 dstx, s16 dsty) const
{
Surface sf(w(), h());
sf.SetColorKey();
sf.Blit(*this);
sf.SetAlpha(alpha);
dst.Blit(sf, dstx, dsty);
}

View File

@@ -1,51 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2SPRITE_H
#define H2SPRITE_H
#include "surface.h"
#include "gamedefs.h"
class Sprite : public Surface
{
public:
Sprite();
void SetOffset(s16, s16);
s16 x(void) const{ return offsetX; }
s16 y(void) const{ return offsetY; }
u32 GetSize(void) const;
void ScaleMinifyByTwo(void);
void BlitSpriteWithAlpha(Surface &, u8, s16, s16) const;
static void DrawICN(Surface & sf, const u8* buf, const u32 size, bool reflect);
static void AddonExtensionModify(Sprite & sp, u16 icn, u16 index);
private:
s16 offsetX;
s16 offsetY;
};
#endif

View File

@@ -1,47 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "surface.h"
#include "til.h"
namespace TIL
{
static const struct
{
til_t type;
const char* string;
} tilmap[] = {
{ CLOF32, "CLOF32.TIL" },
{ GROUND32, "GROUND32.TIL" },
{ STON, "STON.TIL" },
};
}
const char* TIL::GetString(const til_t til)
{
return tilmap[til].string;
}
void TIL::Reflect(Surface & sf_dst, const Surface & sf_src, const u8 shape)
{
Surface::TILReflect(sf_dst, sf_src, shape);
}

View File

@@ -1,45 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2TIL_H
#define H2TIL_H
#include "gamedefs.h"
class Surface;
namespace TIL
{
enum til_t
{
CLOF32,
GROUND32,
STON,
UNKNOWN
};
const char* GetString(const til_t til);
void Reflect(Surface & sf_dst, const Surface & sf_src, const u8 shape);
}
#endif

View File

@@ -1,83 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "xmi.h"
namespace XMI
{
static const struct
{
xmi_t type;
const char* string;
} xmimap[] = {
{ UNKNOWN, "???" },
{ MIDI0002, "MIDI0002.XMI" },
{ MIDI0003, "MIDI0003.XMI" },
{ MIDI0004, "MIDI0004.XMI" },
{ MIDI0005, "MIDI0005.XMI" },
{ MIDI0006, "MIDI0006.XMI" },
{ MIDI0007, "MIDI0007.XMI" },
{ MIDI0008, "MIDI0008.XMI" },
{ MIDI0009, "MIDI0009.XMI" },
{ MIDI0010, "MIDI0010.XMI" },
{ MIDI0011, "MIDI0011.XMI" },
{ MIDI0013, "MIDI0013.XMI" },
{ MIDI0014, "MIDI0014.XMI" },
{ MIDI0015, "MIDI0015.XMI" },
{ MIDI0017, "MIDI0017.XMI" },
{ MIDI0018, "MIDI0018.XMI" },
{ MIDI0042, "MIDI0042.XMI" },
{ MIDI0043, "MIDI0043.XMI" },
};
}
const char* XMI::GetString(const xmi_t xmi)
{
return xmimap[xmi].string;
}
XMI::xmi_t XMI::FromMUS(const MUS::mus_t mus)
{
switch(mus)
{
case MUS::BATTLE1: return MIDI0002;
case MUS::BATTLE2: return MIDI0003;
case MUS::BATTLE3: return MIDI0004;
case MUS::SORCERESS: return MIDI0005;
case MUS::WARLOCK: return MIDI0006;
case MUS::NECROMANCER: return MIDI0007;
case MUS::KNIGHT: return MIDI0008;
case MUS::BARBARIAN: return MIDI0009;
case MUS::WIZARD: return MIDI0010;
case MUS::LAVA: return MIDI0011;
case MUS::DESERT: return MIDI0013;
case MUS::SNOW: return MIDI0014;
case MUS::SWAMP: return MIDI0015;
case MUS::DIRT: return MIDI0017;
case MUS::GRASS: return MIDI0018;
case MUS::MAINMENU: return MIDI0042;
case MUS::VICTORY: return MIDI0043;
default: break;
}
return UNKNOWN;
}

View File

@@ -1,56 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2XMI_H
#define H2XMI_H
#include "mus.h"
namespace XMI
{
enum xmi_t
{
UNKNOWN,
MIDI0002,
MIDI0003,
MIDI0004,
MIDI0005,
MIDI0006,
MIDI0007,
MIDI0008,
MIDI0009,
MIDI0010,
MIDI0011,
MIDI0013,
MIDI0014,
MIDI0015,
MIDI0017,
MIDI0018,
MIDI0042,
MIDI0043
};
const char* GetString(const xmi_t xmi);
xmi_t FromMUS(const MUS::mus_t mus);
}
#endif

View File

@@ -1,24 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "algorithm.h"

View File

@@ -1,38 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2ALGORITHM_H
#define H2ALGORITHM_H
#include <list>
#include "gamedefs.h"
#include "skill.h"
#include "mp2.h"
namespace Route { class Step; }
class Heroes;
namespace Algorithm
{
bool PathFind(std::list<Route::Step> *result, const s32 from, const s32 to, const u16 limit = MAXU16, const Heroes * = NULL);
}
#endif

View File

@@ -1,229 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <cstdlib>
#include <map>
#include "maps.h"
#include "world.h"
#include "direction.h"
#include "settings.h"
#include "object.h"
#include "heroes.h"
#include "route.h"
#include "algorithm.h"
struct cell_t
{
cell_t() : cost_g(MAXU16), cost_t(MAXU16), cost_d(MAXU16), parent(-1), open(true){};
u16 cost_g;
u16 cost_t;
u16 cost_d;
s32 parent;
bool open;
};
bool ImpassableCorners(const s32 from, const Direction::vector_t to, const Heroes *hero)
{
if( to & (Direction::TOP | Direction::BOTTOM | Direction::LEFT | Direction::RIGHT)) return false;
if(to & (Direction::TOP_LEFT | Direction::BOTTOM_LEFT))
{
if(Maps::isValidDirection(from, Direction::LEFT) &&
!world.GetTiles(Maps::GetDirectionIndex(from, Direction::LEFT)).isPassable(hero)) return true;
}
if(to & (Direction::TOP_RIGHT | Direction::BOTTOM_RIGHT))
{
if(Maps::isValidDirection(from, Direction::RIGHT) &&
!world.GetTiles(Maps::GetDirectionIndex(from, Direction::RIGHT)).isPassable(hero)) return true;
}
if(to & (Direction::TOP_LEFT | Direction::TOP_RIGHT))
{
if(Maps::isValidDirection(from, Direction::TOP) &&
!world.GetTiles(Maps::GetDirectionIndex(from, Direction::TOP)).isPassable(hero)) return true;
}
if(to & (Direction::BOTTOM_LEFT | Direction::BOTTOM_RIGHT))
{
if(Maps::isValidDirection(from, Direction::BOTTOM) &&
!world.GetTiles(Maps::GetDirectionIndex(from, Direction::BOTTOM)).isPassable(hero)) return true;
}
return false;
}
u32 GetCurrentLength(std::map<s32, cell_t> & list, s32 cur)
{
u32 res = 0;
const cell_t* cell = &list[cur];
while(-1 != cell->parent){ cell = &list[cell->parent]; ++res; };
return res;
}
bool MonsterDestination(const s32 from, const u16 around, const s32 dst)
{
for(Direction::vector_t dir = Direction::TOP_LEFT; dir < Direction::CENTER; ++dir)
if((around & dir) && dst == Maps::GetDirectionIndex(from, dir))
return true;
return false;
}
bool Algorithm::PathFind(std::list<Route::Step> *result, const s32 from, const s32 to, const u16 limit, const Heroes *hero)
{
const u8 pathfinding = (hero ? hero->GetLevelSkill(Skill::Secondary::PATHFINDING) : Skill::Level::NONE);
const u8 under = (hero ? hero->GetUnderObject() : MP2::OBJ_ZERO);
s32 cur = from;
s32 alt = 0;
s32 tmp = 0;
std::map<s32, cell_t> list;
std::map<s32, cell_t>::iterator it1 = list.begin();
std::map<s32, cell_t>::iterator it2 = list.end();
Direction::vector_t direct = Direction::CENTER;
list[cur].cost_g = 0;
list[cur].cost_t = 0;
list[cur].parent = -1;
list[cur].open = false;
u16 mons = 0;
cell_t cell;
while(cur != to)
{
LocalEvent::Get().HandleEvents(false);
for(direct = Direction::TOP_LEFT; direct != Direction::CENTER; ++direct)
{
if(Maps::isValidDirection(cur, direct))
{
tmp = Maps::GetDirectionIndex(cur, direct);
if(list[tmp].open)
{
// new
if(-1 == list[tmp].parent)
{
cell.cost_g = Maps::Ground::GetPenalty(tmp, direct, pathfinding);
cell.parent = cur;
cell.open = true;
cell.cost_t = cell.cost_g + list[cur].cost_t;
cell.cost_d = 50 * Maps::GetApproximateDistance(tmp, to);
if(MAXU16 == cell.cost_g) continue;
// check monster protection
if(tmp != to && (mons = Maps::TileUnderProtection(tmp)) && ! MonsterDestination(tmp, mons, to)) continue;
// check direct from object
const Maps::Tiles & tile1 = world.GetTiles(cur);
if(MP2::OBJ_ZERO != under && MP2::OBJ_HEROES == tile1.GetObject() && ! Object::AllowDirect(under, direct)) continue;
// check obstacles as corners
//if(ImpassableCorners(cur, direct, hero)) continue; // disable, need fix more objects with passable option
// check direct to object
const Maps::Tiles & tile2 = world.GetTiles(tmp);
if(! Object::AllowDirect(tile2.GetObject(), Direction::Reflect(direct))) continue;
if(tile2.isPassable(hero) || tmp == to) list[tmp] = cell;
}
// check alt
else
{
alt = Maps::Ground::GetPenalty(cur, direct, pathfinding);
if(list[tmp].cost_t > list[cur].cost_t + alt)
{
list[tmp].parent = cur;
list[tmp].cost_g = alt;
list[tmp].cost_t = list[cur].cost_t + alt;
}
}
}
}
}
if(cur == to) break;
else
list[cur].open = false;
DEBUG(DBG_GAME , DBG_TRACE, "Algorithm::PathFind: route, from: " << cur);
it1 = list.begin();
alt = -1;
tmp = MAXU16;
// find minimal cost
for(; it1 != it2; ++it1) if((*it1).second.open)
{
const cell_t & cell2 = (*it1).second;
if(IS_DEBUG(DBG_GAME, DBG_TRACE) && cell2.cost_g != MAXU16)
{
direct = Direction::Get(cur, (*it1).first);
if(Direction::UNKNOWN != direct)
{
std::cout << " direct: " << Direction::String(direct);
std::cout << ", index: " << (*it1).first;
std::cout << ", cost g: " << cell2.cost_g;
std::cout << ", cost t: " << cell2.cost_t;
std::cout << ", cost d: " << cell2.cost_d << std::endl;
}
}
if(cell2.cost_t + cell2.cost_d < tmp)
{
tmp = cell2.cost_t + cell2.cost_d;
alt = (*it1).first;
}
}
// not found, and exception
if(MAXU16 == tmp || -1 == alt || (limit && GetCurrentLength(list, cur) > limit)) break;
else
DEBUG(DBG_GAME , DBG_TRACE, "Algorithm::PathFind: select: " << alt);
cur = alt;
}
// save path
if(cur == to)
{
while(cur != from)
{
if(-1 == list[cur].parent) break;
alt = cur;
cur = list[alt].parent;
if(result) result->push_front(Route::Step(Direction::Get(cur, alt), list[alt].cost_g));
}
return true;
}
DEBUG(DBG_GAME , DBG_TRACE, "Algorithm::PathFind: not found, from:" << from << ", to: " << to);
list.clear();
return false;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,169 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2ARMY_H
#define H2ARMY_H
#include <string>
#include <vector>
#include "bitmodes.h"
#include "heroes_base.h"
#include "army_troop.h"
class Castle;
class Heroes;
namespace Maps { class Tiles; }
namespace Battle2 { class Stats; }
namespace Resource { struct funds_t; }
namespace Army
{
class BattleTroop;
enum format_t
{
FORMAT_GROUPED = 0,
FORMAT_SPREAD = 1
};
enum armysize_t
{
FEW = 1,
SEVERAL = 5,
PACK = 10,
LOTS = 20,
HORDE = 50,
THRONG = 100,
SWARM = 250,
ZOUNDS = 500,
LEGION = 1000
};
const char* String(u32);
armysize_t GetSize(u32);
// 0: fight, 1: free join, 2: join with gold, 3: flee
u8 GetJoinSolution(const Heroes &, const Maps::Tiles &, u32 &, s32 &);
class army_t
{
public:
army_t(HeroBase* s = NULL);
army_t(const army_t &);
army_t & operator= (const army_t &);
void FromGuardian(const Maps::Tiles &);
void UpgradeMonsters(const Monster &);
void UpgradeMonsters(const Monster::monster_t);
void Clear(void);
void Reset(bool = false); // reset: soft or hard
void BattleExportKilled(army_t &) const;
void BattleSetModes(u32);
void BattleResetModes(u32);
Troop* BattleFindModes(u32);
const Troop*BattleFindModes(u32) const;
void BattleInit(void);
void BattleQuit(void);
void BattleNewTurn(void);
Troop & BattleNewTroop(Monster::monster_t, u32);
bool BattleArchersPresent(void) const;
bool BattleDragonsPresent(void) const;
u32 BattleKilled(void) const;
u8 BattleUndeadTroopCount(void) const;
u8 BattleLifeTroopCount(void) const;
const Battle2::Stats* BattleRandomTroop(void) const;
Battle2::Stats* BattleSlowestTroop(bool skipmove);
Battle2::Stats* BattleFastestTroop(bool skipmove);
void DrawMons32Line(s16, s16, u16, u8 = 0, u8 = 0, bool = false) const;
Troop & FirstValid(void);
Troop & At(u8);
Troop & GetWeakestTroop(void);
const Troop & At(u8) const;
const Troop & GetSlowestTroop(void) const;
Race::race_t GetRace(void) const;
Color::color_t GetColor(void) const;
u8 GetControl(void) const;
void SetColor(Color::color_t);
u8 Size(void) const;
u8 GetCount(void) const;
u8 GetUniqCount(void) const;
u32 GetCountMonsters(const Monster &) const;
u32 GetCountMonsters(const Monster::monster_t) const;
s8 GetMorale(void) const;
s8 GetLuck(void) const;
s8 GetMoraleModificator(std::string *strs) const;
s8 GetLuckModificator(std::string *strs) const;
u32 CalculateExperience(void) const;
u32 ActionToSirens(void);
u32 GetSurrenderCost(void) const;
u16 GetAttack(void) const;
u16 GetDefense(void) const;
u32 GetHitPoints(void) const;
u32 GetDamageMin(void) const;
u32 GetDamageMax(void) const;
double GetStrength(void) const;
bool isValid(void) const;
bool HasMonster(const Monster &) const;
bool HasMonster(const Monster::monster_t) const;
bool JoinTroop(const Troop & troop);
bool JoinTroop(const Monster::monster_t mon, const u32 count);
bool JoinTroop(const Monster & mon, const u32 count);
bool StrongerEnemyArmy(const army_t &) const;
bool AllTroopsIsRace(u8) const;
void JoinStrongestFromArmy(army_t &);
void KeepOnlyWeakestTroops(army_t &);
void UpgradeTroops(const Castle &);
void ArrangeForBattle(void);
void Dump(void) const;
const HeroBase* GetCommander(void) const;
HeroBase* GetCommander(void);
void SetCommander(HeroBase*);
void SetCombatFormat(format_t);
u8 GetCombatFormat(void) const;
protected:
friend class Troop;
void Import(army_t &);
void Import(const std::vector<Troop> &);
s8 GetTroopIndex(const Troop &) const;
std::vector<Troop> army;
HeroBase* commander;
u8 combat_format;
Color::color_t color;
};
}
#endif

View File

@@ -1,291 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <algorithm>
#include <functional>
#include "agg.h"
#include "speed.h"
#include "spell.h"
#include "settings.h"
#include "luck.h"
#include "morale.h"
#include "army.h"
#include "maps_tiles.h"
#include "army_troop.h"
#include "battle_stats.h"
Army::Troop::Troop(monster_t m, u32 c) : Monster(m), count(c), army(NULL), battle(NULL)
{
}
Army::Troop::Troop(const Troop & t) : Monster(t.id), count(t.count), army(t.army), battle(NULL)
{
}
Army::Troop::Troop(const Maps::Tiles & t) : army(NULL), battle(NULL)
{
id = Monster::FromInt(t.GetQuantity3());
count = t.GetCountMonster();
}
Army::Troop::~Troop()
{
if(battle) BattleQuit();
}
Army::Troop & Army::Troop::operator= (const Troop & t)
{
id = t.id;
count = t.count;
if(!army && t.army) army = t.army;
if(battle) BattleQuit();
return *this;
}
bool Army::Troop::HasMonster(monster_t m) const
{
return id == m;
}
void Army::Troop::Set(const Monster & m, u32 c)
{
Monster::Set(m);
count = c;
}
void Army::Troop::Set(monster_t m, u32 c)
{
Monster::Set(m);
count = c;
}
void Army::Troop::SetMonster(const Monster & m)
{
Monster::Set(m);
}
void Army::Troop::SetMonster(monster_t m)
{
Monster::Set(m);
}
void Army::Troop::SetCount(u32 c)
{
count = c;
}
void Army::Troop::Reset(void)
{
Monster::Set(Monster::UNKNOWN);
count = 0;
}
const Battle2::Stats* Army::Troop::GetBattleStats(void) const
{
if(!battle) DEBUG(DBG_GAME, DBG_INFO, "Army::Troop::GetBattleStats: return NULL");
return battle;
}
Battle2::Stats* Army::Troop::GetBattleStats(void)
{
return battle;
}
bool Army::Troop::BattleInit(void)
{
if(isValid())
{
if(battle) delete battle;
battle = new Battle2::Stats(*this);
return battle;
}
DEBUG(DBG_BATTLE, DBG_WARN, "Army::Troop::BattleInit: invalid troop");
return false;
}
void Army::Troop::BattleQuit(void)
{
if(battle)
{
if(battle->dead > count)
{
VERBOSE("Army::Troop::BattleQuit: " << GetName() << ", dead(" << battle->dead << ") > current(" << count << ") incorrect!! FIXME!");
count = 0;
}
else
count -= battle->dead;
delete battle;
battle = NULL;
}
}
void Army::Troop::BattleNewTurn(void)
{
if(isValid() && battle) battle->NewTurn();
}
u32 Army::Troop::BattleKilled(void) const
{
return battle ? battle->dead : 0;
}
void Army::Troop::BattleSetModes(u32 f)
{
if(battle) battle->SetModes(f);
}
bool Army::Troop::BattleFindModes(u32 f) const
{
return battle && battle->isValid() && battle->Modes(f);
}
void Army::Troop::BattleResetModes(u32 f)
{
if(battle) battle->ResetModes(f);
}
bool Army::Troop::BattleIsDragons(void) const
{
return battle && battle->isValid() && isDragons();
}
bool Army::Troop::BattleIsArchers(void) const
{
return battle && battle->isValid() && battle->isArchers();
}
const Skill::Primary* Army::Troop::MasterSkill(void) const
{
return army ? army->commander : NULL;
}
Army::army_t* Army::Troop::GetArmy(void)
{
return army;
}
const Army::army_t* Army::Troop::GetArmy(void) const
{
return army;
}
const char* Army::Troop::GetName(u32 amount /* = 0 */) const
{
u32 cmp = count;
if(amount)
cmp = amount;
return 1 < cmp ? Monster::GetMultiName() : Monster::GetName();
}
u32 Army::Troop::GetCount(void) const
{
return count;
}
u8 Army::Troop::GetAttack(void) const
{
return battle ? battle->GetAttack() : Monster::GetAttack() +
(army && army->commander ? army->commander->GetAttack() : 0);
}
u8 Army::Troop::GetDefense(void) const
{
return battle ? battle->GetDefense() : Monster::GetDefense() +
(army && army->commander ? army->commander->GetDefense() : 0);
}
Color::color_t Army::Troop::GetColor(void) const
{
return army ? army->GetColor() : Color::GRAY;
}
u32 Army::Troop::GetHitPoints(void) const
{
return battle ? battle->GetHitPoints() : Monster::GetHitPoints() * count;
}
u32 Army::Troop::GetDamageMin(void) const
{
return Monster::GetDamageMin() * count;
}
u32 Army::Troop::GetDamageMax(void) const
{
return Monster::GetDamageMax() * count;
}
u8 Army::Troop::GetSpeed(void) const
{
return battle ? battle->GetSpeed() : Monster::GetSpeed();
}
s8 Army::Troop::GetMorale(void) const
{
return isAffectedByMorale() && army ? army->GetMorale() : Morale::NORMAL;
}
s8 Army::Troop::GetLuck(void) const
{
return army ? army->GetLuck() : Luck::NORMAL;
}
bool Army::Troop::isValid(void) const
{
return battle ? battle->isValid() : Monster::UNKNOWN < id && count;
}
bool Army::Troop::isAffectedByMorale(void) const
{
return !(isUndead() || isElemental());
}
s8 Army::Troop::GetArmyIndex(void) const
{
return army ? army->GetTroopIndex(*this) : -1;
}
bool Army::isValidTroop(const Troop & troop)
{
return troop.isValid();
}
bool Army::WeakestTroop(const Troop & t1, const Troop & t2)
{
return t1.GetDamageMax() < t2.GetDamageMax();
}
bool Army::StrongestTroop(const Troop & t1, const Troop & t2)
{
return t1.GetDamageMin() > t2.GetDamageMin();
}
bool Army::SlowestTroop(const Troop & t1, const Troop & t2)
{
return t1.GetSpeed() < t2.GetSpeed();
}
void Army::SwapTroops(Troop & t1, Troop & t2)
{
std::swap(t1, t2);
}

View File

@@ -1,115 +0,0 @@
/***************************************************************************
* Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2ARMYTROOP_H
#define H2ARMYTROOP_H
#include <string>
#include "bitmodes.h"
#include "monster.h"
namespace Battle2
{
class Arena;
class Stats;
}
namespace Maps { class Tiles; }
namespace Army
{
class army_t;
class Troop;
bool isValidTroop(const Troop & troop);
bool WeakestTroop(const Troop & t1, const Troop & t2);
bool StrongestTroop(const Troop & t1, const Troop & t2);
bool SlowestTroop(const Troop & t1, const Troop & t2);
void SwapTroops(Troop & t1, Troop & t2);
class Troop : public Monster
{
public:
Troop(monster_t m = Monster::UNKNOWN, u32 c = 0);
Troop(const Troop &);
Troop(const Maps::Tiles &);
~Troop();
Troop & operator= (const Troop &);
void Set(const Monster &, u32);
void Set(monster_t, u32);
void SetMonster(const Monster &);
void SetMonster(monster_t);
void SetCount(u32);
void Reset(void);
const Skill::Primary* MasterSkill(void) const;
army_t* GetArmy(void);
const army_t* GetArmy(void) const;
const char* GetName(u32 amount = 0) const;
u32 Count(void) const { return GetCount(); }
u32 GetCount(void) const;
u8 GetAttack(void) const;
u8 GetDefense(void) const;
u32 GetHitPoints(void) const;
Color::color_t GetColor(void) const;
u8 GetSpeed(void) const;
const Battle2::Stats* GetBattleStats(void) const;
Battle2::Stats* GetBattleStats(void);
u32 GetDamageMin(void) const;
u32 GetDamageMax(void) const;
s8 GetMorale(void) const;
s8 GetLuck(void) const;
bool isValid(void) const;
bool isAffectedByMorale(void) const;
bool HasMonster(monster_t) const;
bool BattleInit(void);
void BattleQuit(void);
void BattleNewTurn(void);
void BattleSetModes(u32);
void BattleResetModes(u32);
bool BattleFindModes(u32) const;
bool BattleIsDragons(void) const;
bool BattleIsArchers(void) const;
u32 BattleKilled(void) const;
s8 GetArmyIndex(void) const;
protected:
friend class army_t;
friend class Battle2::Stats;
friend class Battle2::Arena;
u32 count;
army_t* army;
Battle2::Stats* battle;
};
}
#endif

View File

@@ -1,149 +0,0 @@
/***************************************************************************
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2BATTLE2_H
#define H2BATTLE2_H
#include <vector>
#include <utility>
#include "network.h"
#include "icn.h"
#include "m82.h"
#include "gamedefs.h"
#ifdef WITH_NET
#include "localclient.h"
#endif
namespace Army { class army_t; }
namespace Battle2
{
struct Stats;
enum { RESULT_LOSS = 0x01, RESULT_RETREAT = 0x02, RESULT_SURRENDER = 0x04, RESULT_WINS = 0x80 };
struct Result
{
Result() : army1(0), army2(0), exp1(0), exp2(0) {}
bool AttackerWins(void) const;
bool DefenderWins(void) const;
u8 AttackerResult(void) const;
u8 DefenderResult(void) const;
u32 GetExperienceAttacker(void) const;
u32 GetExperienceDefender(void) const;
u8 army1;
u8 army2;
u32 exp1;
u32 exp2;
};
Result Loader(Army::army_t &, Army::army_t &, s32);
void UpdateMonsterInfoAnimation(const std::string &);
void UpdateMonsterAttributes(const std::string &);
enum direction_t
{
UNKNOWN = 0xFF,
TOP_LEFT = 0x01,
TOP_RIGHT = 0x02,
RIGHT = 0x04,
BOTTOM_RIGHT = 0x08,
BOTTOM_LEFT = 0x10,
LEFT = 0x20,
CENTER = 0x40
};
inline direction_t & operator++ (direction_t & d){ return d = ( CENTER == d ? TOP_LEFT : direction_t(d << 1)); }
inline direction_t & operator-- (direction_t & d){ return d = ( TOP_LEFT == d ? CENTER : direction_t(d >> 1)); }
enum tower_t { TWR_LEFT, TWR_CENTER, TWR_RIGHT };
enum catapult_t { CAT_WALL1 = 1, CAT_WALL2 = 2, CAT_WALL3 = 3, CAT_WALL4 = 4, CAT_TOWER1 = 5, CAT_TOWER2 = 6, CAT_BRIDGE = 7, CAT_TOWER3 = 8, CAT_MISS = 9 };
enum animstate_t
{
AS_NONE,
AS_IDLE,
AS_MOVE,
AS_FLY1,
AS_FLY2,
AS_FLY3,
AS_SHOT0,
AS_SHOT1,
AS_SHOT2,
AS_SHOT3,
AS_ATTK0,
AS_ATTK1,
AS_ATTK2,
AS_ATTK3,
AS_WNCE,
AS_KILL
};
struct animframe_t
{
u8 start;
u8 count;
};
struct MonsterInfo
{
ICN::icn_t icn_file;
animframe_t frm_idle;
animframe_t frm_move;
animframe_t frm_fly1;
animframe_t frm_fly2;
animframe_t frm_fly3;
animframe_t frm_shot0;
animframe_t frm_shot1;
animframe_t frm_shot2;
animframe_t frm_shot3;
animframe_t frm_attk0;
animframe_t frm_attk1;
animframe_t frm_attk2;
animframe_t frm_attk3;
animframe_t frm_wnce;
animframe_t frm_kill;
M82::m82_t m82_attk;
M82::m82_t m82_kill;
M82::m82_t m82_move;
M82::m82_t m82_wnce;
};
struct TargetInfo
{
TargetInfo() : defender(NULL), damage(0), killed(0), resist(false) {};
Stats* defender;
u32 damage;
u32 killed;
bool resist;
bool isFinishAnimFrame(void) const;
};
typedef QueueMessage Action;
}
#endif

View File

@@ -1,276 +0,0 @@
/***************************************************************************
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <algorithm>
#include <functional>
#include "settings.h"
#include "heroes.h"
#include "castle.h"
#include "battle_arena.h"
#include "battle_cell.h"
#include "battle_stats.h"
#include "battle_interface.h"
namespace Battle2
{
bool isApplySpell(const Spell::spell_t, const Stats*, const HeroBase &, Actions &);
}
void Battle2::Arena::AITurn(const Stats & b, Actions & a)
{
DEBUG(DBG_BATTLE, DBG_TRACE, "Battle2::Arena::AITurn: " << \
b.GetName() << "(color: " << Color::String(b.GetColor()) << \
", id: " << b.id << ", pos: " << b.position << ")");
// reset quality param for board
ResetBoard();
// set quality for enemy troop
board.SetEnemyQuality(b);
const Stats* enemy = NULL;
bool attack = false;
if(b.isArchers() && !b.isHandFighting())
{
enemy = GetEnemyMaxQuality(b.GetColor());
AIMagicAction(b, a, enemy);
attack = true;
}
else
if(b.Modes(SP_BERSERKER))
{
std::vector<u16> pos;
GetNearestTroops(b.GetPosition(), pos, NULL);
const u16* move = Rand::Get(pos);
if(move)
{
a.AddedMoveAction(b, *move);
enemy = GetEnemyAbroadMaxQuality(*move, 0);
attack = true;
}
}
else
if(b.isHandFighting())
{
enemy = GetEnemyAbroadMaxQuality(b.GetPosition(), b.GetColor());
AIMagicAction(b, a, enemy);
attack = true;
}
else
{
// set quality position from enemy
board.SetPositionQuality(b);
// get passable quality positions
std::vector<u16> positions;
positions.reserve(30);
GetPassableQualityPositions(b, positions);
attack = true;
if(positions.size())
{
const u16 move = b.AIGetAttackPosition(positions);
if(b.isFly())
{
enemy = GetEnemyAbroadMaxQuality(move, b.GetColor());
AIMagicAction(b, a, enemy);
a.AddedMoveAction(b, move);
attack = true;
}
else
{
std::vector<u16> path;
GetPath(b, move, path);
if(path.size())
{
enemy = GetEnemyAbroadMaxQuality(path.back(), b.GetColor());
AIMagicAction(b, a, enemy);
a.AddedMoveAction(b, path);
// archers move and short attack only
attack = b.isArchers() ? false : true;
}
}
}
else
enemy = GetEnemyAbroadMaxQuality(b);
}
if(enemy)
{
if(attack) a.AddedAttackAction(b, *enemy);
}
else
if(IS_DEBUG(DBG_BATTLE, DBG_TRACE))
{
VERBOSE("Battle2::Arena::AITurn: " << "enemy is NULL");
DumpBoard();
}
// end action
a.AddedEndAction(b);
}
void Battle2::Arena::AIMagicAction(const Stats & b, Actions & a, const Stats* enemy)
{
const HeroBase* hero = b.GetCommander();
if(b.Modes(SP_BERSERKER) || !hero || hero->Modes(Heroes::SPELLCASTED) || !hero->HaveSpellBook()) return;
const Army::army_t* my_army = b.GetArmy();
const Army::army_t* enemy_army = GetArmy(GetOppositeColor(b.GetColor()));
const Army::Troop* troop = NULL;
const Stats* stats = NULL;
// troop bad spell - clean
if(b.Modes(IS_BAD_MAGIC))
{
if(isApplySpell(Spell::DISPEL, &b, *hero, a)) return;
if(isApplySpell(Spell::CURE, &b, *hero, a)) return;
}
if(enemy)
{
// curse
if(!enemy->Modes(SP_CURSE) && isApplySpell(Spell::CURSE, enemy, *hero, a)) return;
// enemy good spell - clean
if(enemy->Modes(IS_GOOD_MAGIC) && isApplySpell(Spell::DISPEL, enemy, *hero, a)) return;
// up defense
if(!b.Modes(SP_STEELSKIN) && !b.Modes(SP_STONESKIN) && isApplySpell(Spell::STEELSKIN, &b, *hero, a)) return;
if(!b.Modes(SP_STONESKIN) && !b.Modes(SP_STEELSKIN) && isApplySpell(Spell::STONESKIN, &b, *hero, a)) return;
}
// my army blessing
if(b.isArchers() || enemy)
{
if(enemy->troop.isDragons() && !b.Modes(SP_DRAGONSLAYER) && isApplySpell(Spell::DRAGONSLAYER, &b, *hero, a)) return;
if(!b.Modes(SP_BLESS) && isApplySpell(Spell::BLESS, &b, *hero, a)) return;
if(!b.Modes(SP_BLOODLUST) && isApplySpell(Spell::BLOODLUST, &b, *hero, a)) return;
if(!b.Modes(SP_HASTE) && isApplySpell(Spell::HASTE, &b, *hero, a)) return;
}
// shield spell conditions
if(NULL != enemy_army)
{
// find archers
if(enemy_army->BattleArchersPresent() ||
// or archers tower
(castle && castle->GetColor() != b.GetColor() && castle->isCastle()))
{
if(!b.Modes(SP_SHIELD) && isApplySpell(Spell::SHIELD, &b, *hero, a)) return;
}
}
// my army scan - clean
if(NULL != my_army && NULL != (troop = my_army->BattleFindModes(IS_BAD_MAGIC)))
{
if(NULL != (stats = troop->GetBattleStats()))
{
if(isApplySpell(Spell::DISPEL, stats, *hero, a)) return;
if(isApplySpell(Spell::CURE, stats, *hero, a)) return;
}
}
// enemy army spell
if(NULL != enemy_army)
{
// find mirror image or summon elem
if(NULL != (troop = enemy_army->BattleFindModes(CAP_MIRRORIMAGE | CAP_SUMMONELEM)) && NULL != (stats = troop->GetBattleStats()))
{
if(isApplySpell(Spell::ARROW, stats, *hero, a)) return;
if(isApplySpell(Spell::COLDRAY, stats, *hero, a)) return;
if(isApplySpell(Spell::FIREBALL, stats, *hero, a)) return;
if(isApplySpell(Spell::LIGHTNINGBOLT, stats, *hero, a)) return;
}
// find good magic
if(NULL != (troop = enemy_army->BattleFindModes(IS_GOOD_MAGIC)) && NULL != (stats = troop->GetBattleStats()))
{
// slow
if(stats->Modes(SP_HASTE) && isApplySpell(Spell::SLOW, stats, *hero, a)) return;
// curse
if(!stats->Modes(SP_CURSE) && isApplySpell(Spell::CURSE, stats, *hero, a)) return;
//
if(isApplySpell(Spell::DISPEL, stats, *hero, a)) return;
}
// check undead
if(my_army->BattleUndeadTroopCount() < enemy_army->BattleUndeadTroopCount())
{
if(isApplySpell(Spell::HOLYSHOUT, NULL, *hero, a)) return;
if(isApplySpell(Spell::HOLYWORD, NULL, *hero, a)) return;
}
// check alife
if(my_army->BattleLifeTroopCount() < enemy_army->BattleLifeTroopCount())
{
if(isApplySpell(Spell::DEATHRIPPLE, NULL, *hero, a)) return;
if(isApplySpell(Spell::DEATHWAVE, NULL, *hero, a)) return;
}
stats = enemy_army->BattleRandomTroop();
if(isApplySpell(Spell::LIGHTNINGBOLT, stats, *hero, a)) return;
if(isApplySpell(Spell::FIREBALL, stats, *hero, a)) return;
if(isApplySpell(Spell::COLDRAY, stats, *hero, a)) return;
if(isApplySpell(Spell::ARROW, stats, *hero, a)) return;
}
/* FIX: Battle2::Arena: AIMagicAction:Damage Spell:
Spell::FIREBLAST
Spell::COLDRING
Spell::CHAINLIGHTNING
Spell::METEORSHOWER
*/
if(isApplySpell(Spell::ARMAGEDDON, NULL, *hero, a)) return;
if(isApplySpell(Spell::ELEMENTALSTORM, NULL, *hero, a)) return;
}
bool Battle2::isApplySpell(const Spell::spell_t spell, const Stats* b, const HeroBase & hero, Actions & a)
{
switch(spell)
{
case Spell::CURE: if(isApplySpell(Spell::MASSCURE, b, hero, a)) return true; break;
case Spell::HASTE: if(isApplySpell(Spell::MASSHASTE, b, hero, a)) return true; break;
case Spell::SLOW: if(isApplySpell(Spell::MASSSLOW, b, hero, a)) return true; break;
case Spell::BLESS: if(isApplySpell(Spell::MASSBLESS, b, hero, a)) return true; break;
case Spell::CURSE: if(isApplySpell(Spell::MASSCURSE, b, hero, a)) return true; break;
case Spell::DISPEL: if(isApplySpell(Spell::MASSDISPEL, b, hero, a)) return true; break;
case Spell::SHIELD: if(isApplySpell(Spell::MASSSHIELD, b, hero, a)) return true; break;
default: break;
}
if(hero.HaveSpell(spell) && hero.HaveSpellPoints(Spell::CostManaPoints(spell, &hero)) && (!b || b->AllowApplySpell(spell, &hero)))
{
a.AddedCastAction(spell, (b ? b->GetPosition() : MAXU16));
return true;
}
return false;
}

View File

@@ -1,237 +0,0 @@
/***************************************************************************
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2BATTLE2_ARENA_H
#define H2BATTLE2_ARENA_H
#include <list>
#include <map>
#include "battle2.h"
#include "gamedefs.h"
#define ARENAW 11
#define ARENAH 9
#define ARENASIZE ARENAW * ARENAH
namespace Army { class Troop; }
class Castle;
class HeroBase;
namespace Battle2
{
class Arena;
class Cell;
class Stats;
class Tower;
class Catapult;
class Bridge;
class Interface;
struct Actions : public std::list<Action>
{
void AddedRetreatAction(void);
void AddedSurrenderAction(void);
void AddedCastAction(u8, u16);
void AddedCastTeleportAction(u16, u16);
void AddedEndAction(const Stats &);
void AddedSkipAction(const Stats &, bool);
void AddedMoveAction(const Stats &, u16);
void AddedMoveAction(const Stats &, const std::vector<u16> &);
void AddedAttackAction(const Stats &, const Stats &);
void AddedMoraleAction(const Stats &, u8);
};
struct Board : public std::vector<Cell>
{
Board();
s16 GetIndexAbsPosition(const Point &) const;
Rect GetArea(void) const;
void SetEnemyQuality(const Stats &);
void SetPositionQuality(const Stats &);
void SetCobjObjects(s32);
void SetCobjObject(u16, u16);
void SetCovrObjects(u16);
void GetAbroadPositions(u16, u8, std::vector<u16> &) const;
void GetIndexesFromAbsPoints(std::vector<u16> &, const std::vector<Point> &) const;
static bool inCastle(u16);
static bool isMoatIndex(u16);
static direction_t GetReflectDirection(u8);
static direction_t GetDirection(u16, u16);
static u16 GetDistance(u16, u16);
static bool isValidDirection(u16, u8);
static u16 GetIndexDirection(u16, u8);
};
class GraveyardTroop
{
public:
GraveyardTroop(const Arena &);
void GetClosedCells(std::vector<u16> &) const;
u16 GetLastTroopIDFromCell(u16) const;
void AddTroopID(u16);
void RemoveTroopID(u16);
private:
std::map<u16, std::vector<u16> > map;
const Arena & arena;
};
class Arena
{
public:
Arena(Army::army_t &, Army::army_t &, s32, bool);
~Arena();
void Turns(u16, Result &);
void RemoteTurn(const Stats &, Actions &);
void HumanTurn(const Stats &, Actions &);
const Cell* GetCell(u16, direction_t = CENTER) const;
Cell* GetCell(u16, direction_t = CENTER);
Stats* GetTroopBoard(u16);
const Stats* GetTroopBoard(u16) const;
Stats* GetTroopID(u16);
const Stats* GetTroopID(u16) const;
Stats* GetEnemyAbroadMaxQuality(u16, u8);
const Stats* GetEnemyAbroadMaxQuality(u16, u8) const;
Stats* GetEnemyAbroadMaxQuality(const Stats &);
const Stats* GetEnemyAbroadMaxQuality(const Stats &) const;
Stats* GetEnemyMaxQuality(u8);
const Stats* GetEnemyMaxQuality(u8) const;
Army::army_t* GetArmy(u8);
const Army::army_t* GetArmy(u8) const;
void GetArmyPositions(u8, std::vector<u16> &) const;
u16 GetMaxQualityPosition(const std::vector<u16> &) const;
u16 GetNearestTroops(u16, std::vector<u16> &, const std::vector<u16>* black = NULL) const;
void DialogBattleSummary(const Result &) const;
u8 DialogBattleHero(const HeroBase &) const;
void FadeArena(void) const;
const std::vector<u8> & GetUsageSpells(void) const;
void AddSpell(u8);
u16 GetPath(const Stats &, u16, std::vector<u16> &);
void DumpBoard(void) const;
Interface* GetInterface(void);
Tower* GetTower(u8);
void ApplyAction(Action &);
void GetTargetsForDamage(Stats &, Stats &, std::vector<TargetInfo> &);
void TargetsApplyDamage(Stats &, Stats &, std::vector<TargetInfo> &);
void GetTargetsForSpells(const HeroBase*, const u8, const u16, std::vector<TargetInfo> &);
void TargetsApplySpell(const HeroBase*, const u8, std::vector<TargetInfo> &);
void UnpackBoard(Action &);
void PackBoard(Action &) const;
u8 GetCastleTargetValue(u8) const;
void SetCastleTargetValue(u8, u8);
bool isDisableCastSpell(u8 spell, std::string *msg) const;
bool isAllowResurrectFromGraveyard(u8, u16) const;
u8 GetOppositeColor(u8) const;
void TowerAction(void);
void CatapultAction(void);
bool CanSurrenderOpponent(u8 color) const;
bool CanRetreatOpponent(u8 color) const;
void ResetBoard(void);
void ScanPassabilityBoard(const Stats &, bool skip_speed = false);
static u16 GetShortDistance(u16, const std::vector<u16> &);
void GetPassableQualityPositions(const Stats &, std::vector<u16> &);
u16 GetFreePositionNearHero(u8) const;
// uniq spells
void SpellActionSummonElemental(const HeroBase*, u8);
void SpellActionTeleport(u16, u16);
void SpellActionEarthQuake(void);
void SpellActionMirrorImage(Stats &);
// battle_ai
void AITurn(const Stats &, Actions &);
void AIMagicAction(const Stats &, Actions &, const Stats*);
// battle_action
void ApplyActionRetreat(Action &);
void ApplyActionSurrender(Action &);
void ApplyActionAttack(Action &);
void ApplyActionMove(Action &);
void ApplyActionEnd(Action &);
void ApplyActionSkip(Action &);
void ApplyActionMorale(Action &);
void ApplyActionLuck(Action &);
void ApplyActionSpellCast(Action &);
void ApplyActionTower(Action &);
void ApplyActionCatapult(Action &);
void BattleProcess(Stats &, Stats &);
protected:
friend class Interface;
friend class Cell;
friend class Stats;
friend class Tower;
friend class Bridge;
friend class Catapult;
Army::army_t & army1;
Army::army_t & army2;
const Castle* castle;
HeroBase* current_commander;
Tower* towers[3];
Catapult* catapult;
Bridge* bridge;
Interface* interface;
Result *result_game;
GraveyardTroop graveyard;
std::vector<u8> usage_spells;
Board board;
ICN::icn_t icn_covr;
};
}
#endif

View File

@@ -1,110 +0,0 @@
/***************************************************************************
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "castle.h"
#include "battle_stats.h"
#include "battle_cell.h"
#include "battle_bridge.h"
Battle2::Bridge::Bridge(Arena & a) : arena(a), destroy(false), down(false)
{
}
bool Battle2::Bridge::isIndex(u16 index)
{
switch(index)
{
case 49:
case 50: return true;
default: break;
}
return false;
}
bool Battle2::Bridge::isValid(void) const
{
return !isDestroy();
}
bool Battle2::Bridge::isDestroy(void) const
{
return destroy;
}
bool Battle2::Bridge::isDown(void) const
{
return down || isDestroy();
}
void Battle2::Bridge::SetDown(bool f)
{
down = f;
}
bool Battle2::Bridge::AllowUp(void)
{
return NULL == arena.GetTroopBoard(49) && NULL == arena.GetTroopBoard(50);
}
bool Battle2::Bridge::NeedDown(const Stats & b, u16 pos2)
{
const u16 pos1 = b.GetPosition();
if(pos2 == 50)
{
if(pos1 == 51) return true;
if((pos1 == 61 || pos1 == 39) && b.GetColor() == arena.castle->GetColor()) return true;
}
else
if(pos2 == 49)
{
if(pos1 != 50 && b.GetColor() == arena.castle->GetColor()) return true;
}
return false;
}
bool Battle2::Bridge::isPassable(u8 color) const
{
return color == arena.castle->GetColor() || isDown();
}
void Battle2::Bridge::SetDestroy(void)
{
destroy = true;
arena.board[49].object = 0;
arena.board[50].object = 0;
}
void Battle2::Bridge::SetPassable(const Stats & b)
{
if(Board::inCastle(b.GetPosition()) || b.GetColor() == arena.castle->GetColor())
{
arena.board[49].object = 0;
arena.board[50].object = 0;
}
else
{
arena.board[49].object = 1;
arena.board[50].object = 1;
}
}

View File

@@ -1,58 +0,0 @@
/***************************************************************************
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2BATTLE2_BRIDGE_H
#define H2BATTLE2_BRIDGE_H
#include "gamedefs.h"
#include "battle_arena.h"
namespace Battle2
{
class Stats;
class Bridge
{
public:
Bridge(Arena & a);
void SetDestroy(void);
void SetDown(bool);
void SetPassable(const Stats &);
bool AllowUp(void);
bool NeedDown(const Stats &, u16);
bool isPassable(u8) const;
bool isValid(void) const;
bool isDestroy(void) const;
bool isDown(void) const;
static bool isIndex(u16);
private:
Arena & arena;
bool destroy;
bool down;
};
}
#endif

View File

@@ -1,188 +0,0 @@
/***************************************************************************
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "castle.h"
#include "artifact.h"
#include "skill.h"
#include "settings.h"
#include "heroes_base.h"
#include "battle_arena.h"
#include "battle_cell.h"
#include "battle_tower.h"
#include "battle_bridge.h"
#include "battle_catapult.h"
Battle2::Catapult::Catapult(const HeroBase & hero, bool fortification, Arena & a) : arena(a), cat_shots(1), cat_first(20), cat_miss(true), cat_fort(fortification)
{
switch(hero.GetLevelSkill(Skill::Secondary::BALLISTICS))
{
case Skill::Level::BASIC:
cat_first = 40;
cat_miss = false;
break;
case Skill::Level::ADVANCED:
cat_first = 80;
cat_shots += 1;
cat_miss = false;
break;
case Skill::Level::EXPERT:
cat_first = 100;
cat_shots += 1;
cat_miss = false;
break;
default: break;
}
if(hero.HasArtifact(Artifact::BALLISTA)) cat_shots += 1;
}
u8 Battle2::Catapult::GetShots(void) const
{
return cat_shots;
}
u8 Battle2::Catapult::GetDamage(u8 target)
{
u8 value = arena.GetCastleTargetValue(target);
switch(target)
{
case CAT_WALL1:
case CAT_WALL2:
case CAT_WALL3:
case CAT_WALL4:
if(value)
{
if(cat_first == 100 || cat_first >= Rand::Get(1, 100))
{
// value = value;
DEBUG(DBG_BATTLE, DBG_TRACE, "Battle2::Catapult::GetDamage: " << "from one blow capability");
}
else
value = 1;
}
break;
case CAT_MISS: DEBUG(DBG_BATTLE, DBG_TRACE, "Battle2::Catapult::GetDamage: " << " miss!"); break;
default: break;
}
return value;
}
Point Battle2::Catapult::GetTargetPosition(u8 target) const
{
Point res;
switch(target)
{
case CAT_WALL1: res = Point(475, 45); break;
case CAT_WALL2: res = Point(420, 115); break;
case CAT_WALL3: res = Point(415, 280); break;
case CAT_WALL4: res = Point(490, 390); break;
case CAT_TOWER1:res = Point(430, 40); break;
case CAT_TOWER2:res = Point(430, 300); break;
case CAT_TOWER3:res = Point(580, 160); break;
case CAT_BRIDGE:res = Point(400, 195); break;
case CAT_MISS: res = Point(610, 320); break;
default: break;
}
return res;
}
u8 Battle2::Catapult::GetTarget(const std::vector<u8> & values) const
{
std::vector<u8> targets;
targets.reserve(4);
// check walls
if(0 != values[CAT_WALL1]) targets.push_back(CAT_WALL1);
if(0 != values[CAT_WALL2]) targets.push_back(CAT_WALL2);
if(0 != values[CAT_WALL3]) targets.push_back(CAT_WALL3);
if(0 != values[CAT_WALL4]) targets.push_back(CAT_WALL4);
// check right/left towers
if(targets.empty())
{
if(values[CAT_TOWER1]) targets.push_back(CAT_TOWER1);
if(values[CAT_TOWER2]) targets.push_back(CAT_TOWER2);
}
// check bridge
if(targets.empty())
{
if(values[CAT_BRIDGE]) targets.push_back(CAT_BRIDGE);
}
// check general tower
if(targets.empty())
{
if(values[CAT_TOWER3]) targets.push_back(CAT_TOWER3);
}
if(targets.size())
{
// miss for 30%
return cat_miss && 7 > Rand::Get(1, 20) ? CAT_MISS : (1 < targets.size() ? *Rand::Get(targets) : targets.front());
}
DEBUG(DBG_BATTLE, DBG_TRACE, "Battle2::Catapult::ApplyDamage: " << "target not found..");
return 0;
}
void Battle2::Catapult::Action(void)
{
Battle2::Action action;
action.SetID(MSG_BATTLE_CATAPULT);
u8 shots = GetShots();
std::vector<u8> values;
values.resize(CAT_MISS, 0);
values[CAT_WALL1] = arena.GetCastleTargetValue(CAT_WALL1);
values[CAT_WALL2] = arena.GetCastleTargetValue(CAT_WALL2);
values[CAT_WALL3] = arena.GetCastleTargetValue(CAT_WALL3);
values[CAT_WALL4] = arena.GetCastleTargetValue(CAT_WALL4);
values[CAT_TOWER1] = arena.GetCastleTargetValue(CAT_TOWER1);
values[CAT_TOWER2] = arena.GetCastleTargetValue(CAT_TOWER2);
values[CAT_TOWER3] = arena.GetCastleTargetValue(CAT_TOWER3);
values[CAT_BRIDGE] = arena.GetCastleTargetValue(CAT_BRIDGE);
action.Push(shots);
while(shots--)
{
const u8 target = GetTarget(values);
const u8 damage = GetDamage(target);
action.Push(target);
action.Push(damage);
values[target] -= damage;
}
arena.ApplyAction(action);
}

View File

@@ -1,52 +0,0 @@
/***************************************************************************
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2BATTLE2_CATAPULT_H
#define H2BATTLE2_CATAPULT_H
#include "gamedefs.h"
class HeroBase;
namespace Battle2
{
class Catapult
{
public:
Catapult(const HeroBase &, bool, Arena &);
Point GetTargetPosition(u8) const;
u8 GetShots(void) const;
u8 GetTarget(const std::vector<u8> &) const;
u8 GetDamage(u8);
void Action(void);
private:
Arena & arena;
u8 cat_shots;
u8 cat_first;
bool cat_miss;
bool cat_fort;
};
}
#endif

View File

@@ -1,251 +0,0 @@
/***************************************************************************
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "army_troop.h"
#include "battle_arena.h"
#include "settings.h"
#include "battle_stats.h"
#include "battle_cell.h"
#define CELLW 45
#define CELLH 52
#define CELLW2 23
#define CELLH2 26
Battle2::Cell::Cell(u16 i, const Rect* area, Arena & a) : index(i), arena(&a)
{
object = 0;
direction = UNKNOWN;
quality = 0;
if(Settings::Get().QVGA())
{
pos.x = (area ? area->x : 0) + 45 - ((i / ARENAW) % 2 ? CELLW2 / 2 : 0) + (CELLW2 - 1) * (i % ARENAW);
pos.y = (area ? area->y + area->h - 188 : 0) + ((CELLH2 / 4) * 3) * (i / ARENAW);
pos.w = CELLW2;
pos.h = CELLH2;
}
else
{
pos.x = (area ? area->x : 0) + 88 - ((i / ARENAW) % 2 ? CELLW / 2 : 0) + (CELLW - 1) * (i % ARENAW);
pos.y = (area ? area->y : 0) + 85 + ((CELLH / 4) * 3 - 1) * (i / ARENAW);
pos.w = CELLW;
pos.h = CELLH;
}
}
bool Battle2::Cell::isPositionIncludePoint(const Point & pt) const
{
return pos & pt;
}
u16 Battle2::Cell::GetIndex(void) const
{
return index;
}
u8 Battle2::Cell::GetDirection(void) const
{
return direction;
}
const Rect & Battle2::Cell::GetPos(void) const
{
return pos;
}
bool GetReflectFromDirection(Battle2::direction_t dir)
{
switch(dir)
{
case Battle2::TOP_LEFT:
case Battle2::BOTTOM_LEFT:
case Battle2::LEFT:
return true;
default: break;
}
return false;
}
bool Battle2::Cell::isPassable(const Stats & b, const Cell & from) const
{
if(b.isWide())
{
switch(Board::GetDirection(from.index, index))
{
case BOTTOM_RIGHT:
case TOP_RIGHT:
{
if(CENTER == from.direction && b.isReflect()) return false;
else
if(GetReflectFromDirection(Board::GetReflectDirection(from.direction))) return false;
const Cell* cell2 = arena->GetCell(index, LEFT);
return cell2 && cell2->isPassable() && isPassable();
}
case BOTTOM_LEFT:
case TOP_LEFT:
{
if(CENTER == from.direction && !b.isReflect()) return false;
else
if(!GetReflectFromDirection(Board::GetReflectDirection(from.direction))) return false;
const Cell* cell2 = arena->GetCell(index, RIGHT);
return cell2 && cell2->isPassable() && isPassable();
}
case LEFT:
case RIGHT:
return isPassable() || index == b.GetTailIndex() || index == b.GetPosition();
default: break;
}
}
return isPassable();
}
bool Battle2::Cell::isPassable(const Stats & b, bool check_reflect) const
{
if(b.isWide())
{
if(index == b.GetTailIndex() || index == b.GetPosition()) return true;
if(check_reflect)
{
const Cell* cell = arena->GetCell(index, b.isReflect() ? RIGHT : LEFT);
return cell &&
(cell->isPassable() || cell->index == b.GetTailIndex() || cell->index == b.GetPosition()) &&
isPassable();
}
else
{
Cell* left = arena->GetCell(index, LEFT);
Cell* right = arena->GetCell(index, RIGHT);
return ((left && (left->isPassable() || left->index == b.GetTailIndex() || left->index == b.GetPosition())) ||
(right && (right->isPassable() || right->index == b.GetTailIndex() || right->index == b.GetPosition()))) &&
isPassable();
}
}
return isPassable();
}
bool Battle2::Cell::isPassable(void) const
{
return NULL == arena->GetTroopBoard(index) && 0 == object;
}
void Battle2::Cell::Reset(void)
{
direction = UNKNOWN;
quality = 0;
}
void Battle2::Cell::ResetDirection(void)
{
direction = UNKNOWN;
}
void Battle2::Cell::SetEnemyQuality(const Stats & my)
{
const Stats* b = arena->GetTroopBoard(index);
if(b && b->GetColor() != my.GetColor() && my.isValid())
{
// strength monster quality
quality = b->GetScoreQuality(my) * b->HowMuchWillKilled(my.GetDamage(*b));
quality += b->GetExtraQuality(quality);
if(quality < 1) quality = 1;
DEBUG(DBG_BATTLE, DBG_TRACE, "Battle2::Cell::SetEnemyQuality: " << quality << " for " << b->GetName());
}
}
void Battle2::Cell::SetPositionQuality(const Stats & a)
{
const Stats* b = arena->GetTroopBoard(index);
if(b && b->isWide() && b->GetTailIndex() == index) return;
if(b && b->GetColor() != a.GetColor())
{
for(direction_t dir = TOP_LEFT; dir < CENTER; ++dir)
{
Cell* cell = arena->GetCell(index, dir);
if(cell && cell->isPassable()) cell->quality += quality;
}
// update for wide
if(b->isWide())
{
Cell* cell = NULL;
if(b->isReflect())
{
cell = arena->GetCell(b->GetTailIndex(), TOP_RIGHT);
if(cell && cell->isPassable()) cell->quality += quality;
cell = arena->GetCell(b->GetTailIndex(), RIGHT);
if(cell && cell->isPassable()) cell->quality += quality;
cell = arena->GetCell(b->GetTailIndex(), BOTTOM_RIGHT);
if(cell && cell->isPassable()) cell->quality += quality;
}
else
{
cell = arena->GetCell(b->GetTailIndex(), TOP_LEFT);
if(cell && cell->isPassable()) cell->quality += quality;
cell = arena->GetCell(b->GetTailIndex(), LEFT);
if(cell && cell->isPassable()) cell->quality += quality;
cell = arena->GetCell(b->GetTailIndex(), BOTTOM_LEFT);
if(cell && cell->isPassable()) cell->quality += quality;
}
}
}
}
void Battle2::Cell::SetPassabilityAbroad(const Stats & b, std::vector<u16> & opens)
{
for(direction_t dir = TOP_LEFT; dir < CENTER; ++dir)
{
Cell* cell = arena->GetCell(index, dir);
if(cell && UNKNOWN == cell->direction && cell->isPassable(b, *this))
{
cell->direction = Board::GetReflectDirection(dir);
opens.push_back(cell->index);
}
}
if(b.isWide())
{
Cell* tail = arena->GetCell(b.GetTailIndex());
if(tail)
for(direction_t dir = TOP_LEFT; dir < CENTER; ++dir)
{
Cell* cell = arena->GetCell(tail->index, dir);
if(cell && UNKNOWN == cell->direction && cell->isPassable(b, *tail))
{
cell->direction = Board::GetReflectDirection(dir);
opens.push_back(cell->index);
}
}
}
}

View File

@@ -1,60 +0,0 @@
/***************************************************************************
* Copyright (C) 2010 by Andrey Afletdinov <fheroes2@gmail.com> *
* *
* Part of the Free Heroes2 Engine: *
* http://sourceforge.net/projects/fheroes2 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef H2BATTLE2_CELL_H
#define H2BATTLE2_CELL_H
#include "gamedefs.h"
#include "battle_arena.h"
namespace Army { class Troop; }
namespace Battle2
{
class Cell
{
public:
Cell(u16, const Rect*, Arena &);
void Reset(void);
void ResetDirection(void);
void SetEnemyQuality(const Stats &);
void SetPositionQuality(const Stats &);
bool isPassable(const Stats &, const Cell &) const;
bool isPassable(const Stats &, bool check_reflect) const;
bool isPassable(void) const;
bool isPositionIncludePoint(const Point &) const;
u16 GetIndex(void) const;
u8 GetDirection(void) const;
const Rect & GetPos(void) const;
void SetPassabilityAbroad(const Stats & b, std::vector<u16> &);
u16 index;
Rect pos;
u8 object;
u8 direction;
s32 quality;
Arena* arena;
};
}
#endif

Some files were not shown because too many files have changed in this diff Show More