From ffd335aca284c286030e2b26f1a02a0441748f46 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 27 Aug 2013 11:39:37 -0400 Subject: Started rewriting game from scratch with SDL2 Only the title screen is currently implemented --- .hgignore | 1 + Makefile.am | 2 +- acinclude.m4 | 129 +++++++++++++---------- chlstate.cpp | 50 --------- chlstate.h | 16 --- configure.ac | 42 ++++---- gamestate.cpp | 317 --------------------------------------------------------- gamestate.h | 59 ----------- highscore.cpp | 2 +- hsglobal.cpp | 63 ------------ hsglobal.h | 18 ---- hslist.cpp | 15 ++- hslist.h | 4 + hslocal.cpp | 82 --------------- hslocal.h | 18 ---- hsnew.cpp | 220 --------------------------------------- hsnew.h | 35 ------- hssubmit.cpp | 226 ---------------------------------------- hssubmit.h | 34 ------- htpstate.cpp | 57 ----------- htpstate.h | 18 ---- icon.ico | Bin 6766 -> 0 bytes includes.h | 23 ----- mazeoflife.cpp | 141 ++++++------------------- mazeoflife.h | 8 -- state.h | 8 +- titlestate.cpp | 84 +++++++-------- titlestate.h | 14 +-- util.cpp | 69 +++++++++++++ util.h | 14 +++ winres.rc | 1 - 31 files changed, 270 insertions(+), 1500 deletions(-) delete mode 100644 chlstate.cpp delete mode 100644 chlstate.h delete mode 100644 gamestate.cpp delete mode 100644 gamestate.h delete mode 100644 hsglobal.cpp delete mode 100644 hsglobal.h delete mode 100644 hslocal.cpp delete mode 100644 hslocal.h delete mode 100644 hsnew.cpp delete mode 100644 hsnew.h delete mode 100644 hssubmit.cpp delete mode 100644 hssubmit.h delete mode 100644 htpstate.cpp delete mode 100644 htpstate.h delete mode 100644 icon.ico delete mode 100644 includes.h create mode 100644 util.cpp create mode 100644 util.h delete mode 100644 winres.rc diff --git a/.hgignore b/.hgignore index 85018a3..45a2263 100644 --- a/.hgignore +++ b/.hgignore @@ -11,3 +11,4 @@ missing install-sh depcomp mazeoflife +Makefile diff --git a/Makefile.am b/Makefile.am index f1b00a1..803036c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,6 +2,6 @@ AUTOMAKE_OPTIONS = subdir-objects ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} bin_PROGRAMS = mazeoflife -mazeoflife_SOURCES = chlstate.cpp gamestate.cpp highscore.cpp hsglobal.cpp hslist.cpp hslocal.cpp hsnew.cpp hssubmit.cpp htpstate.cpp mazeoflife.cpp titlestate.cpp +mazeoflife_SOURCES = highscore.cpp hslist.cpp mazeoflife.cpp util.cpp titlestate.cpp AM_CXXFLAGS = $(SDLTTF_CXXFLAGS) $(SDLNET_CXXFLAGS) mazeoflife_LDADD = $(SDLTTF_LIBS) $(SDLNET_LIBS) diff --git a/acinclude.m4 b/acinclude.m4 index 0d44c45..a03b2d2 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -7,12 +7,12 @@ # serial 1 -dnl AM_PATH_SDL([MINIMUM-VERSION, [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]]) +dnl AM_PATH_SDL2([MINIMUM-VERSION, [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]]) dnl Test for SDL, and define SDL_CFLAGS and SDL_LIBS dnl -AC_DEFUN([AM_PATH_SDL], +AC_DEFUN([AM_PATH_SDL2], [dnl -dnl Get the cflags and libraries from the sdl-config script +dnl Get the cflags and libraries from the sdl2-config script dnl AC_ARG_WITH(sdl-prefix,[ --with-sdl-prefix=PFX Prefix where SDL is installed (optional)], sdl_prefix="$withval", sdl_prefix="") @@ -21,50 +21,63 @@ AC_ARG_WITH(sdl-exec-prefix,[ --with-sdl-exec-prefix=PFX Exec prefix where SDL AC_ARG_ENABLE(sdltest, [ --disable-sdltest Do not try to compile and run a test SDL program], , enable_sdltest=yes) - if test x$sdl_exec_prefix != x ; then - sdl_config_args="$sdl_config_args --exec-prefix=$sdl_exec_prefix" - if test x${SDL_CONFIG+set} != xset ; then - SDL_CONFIG=$sdl_exec_prefix/bin/sdl-config + min_sdl_version=ifelse([$1], ,2.0.0,$1) + + if test "x$sdl_prefix$sdl_exec_prefix" = x ; then + PKG_CHECK_MODULES([SDL], [sdl2 >= $min_sdl_version], + [sdl_pc=yes], + [sdl_pc=no]) + else + sdl_pc=no + if test x$sdl_exec_prefix != x ; then + sdl_config_args="$sdl_config_args --exec-prefix=$sdl_exec_prefix" + if test x${SDL2_CONFIG+set} != xset ; then + SDL2_CONFIG=$sdl_exec_prefix/bin/sdl2-config + fi fi - fi - if test x$sdl_prefix != x ; then - sdl_config_args="$sdl_config_args --prefix=$sdl_prefix" - if test x${SDL_CONFIG+set} != xset ; then - SDL_CONFIG=$sdl_prefix/bin/sdl-config + if test x$sdl_prefix != x ; then + sdl_config_args="$sdl_config_args --prefix=$sdl_prefix" + if test x${SDL2_CONFIG+set} != xset ; then + SDL2_CONFIG=$sdl_prefix/bin/sdl2-config + fi fi fi - as_save_PATH="$PATH" - if test "x$prefix" != xNONE; then - PATH="$prefix/bin:$prefix/usr/bin:$PATH" - fi - AC_PATH_PROG(SDL_CONFIG, sdl-config, no, [$PATH]) - PATH="$as_save_PATH" - min_sdl_version=ifelse([$1], ,0.11.0,$1) - AC_MSG_CHECKING(for SDL - version >= $min_sdl_version) - no_sdl="" - if test "$SDL_CONFIG" = "no" ; then - no_sdl=yes + if test "x$sdl_pc" = xyes ; then + no_sdl="" + SDL2_CONFIG="pkg-config sdl2" else - SDL_CFLAGS=`$SDL_CONFIG $sdl_config_args --cflags` - SDL_LIBS=`$SDL_CONFIG $sdl_config_args --libs` + as_save_PATH="$PATH" + if test "x$prefix" != xNONE && test "$cross_compiling" != yes; then + PATH="$prefix/bin:$prefix/usr/bin:$PATH" + fi + AC_PATH_PROG(SDL2_CONFIG, sdl2-config, no, [$PATH]) + PATH="$as_save_PATH" + AC_MSG_CHECKING(for SDL - version >= $min_sdl_version) + no_sdl="" - sdl_major_version=`$SDL_CONFIG $sdl_config_args --version | \ - sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'` - sdl_minor_version=`$SDL_CONFIG $sdl_config_args --version | \ - sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'` - sdl_micro_version=`$SDL_CONFIG $sdl_config_args --version | \ - sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'` - if test "x$enable_sdltest" = "xyes" ; then - ac_save_CFLAGS="$CFLAGS" - ac_save_CXXFLAGS="$CXXFLAGS" - ac_save_LIBS="$LIBS" - CFLAGS="$CFLAGS $SDL_CFLAGS" - CXXFLAGS="$CXXFLAGS $SDL_CFLAGS" - LIBS="$LIBS $SDL_LIBS" + if test "$SDL2_CONFIG" = "no" ; then + no_sdl=yes + else + SDL_CFLAGS=`$SDL2_CONFIG $sdl_config_args --cflags` + SDL_LIBS=`$SDL2_CONFIG $sdl_config_args --libs` + + sdl_major_version=`$SDL2_CONFIG $sdl_config_args --version | \ + sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'` + sdl_minor_version=`$SDL2_CONFIG $sdl_config_args --version | \ + sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'` + sdl_micro_version=`$SDL2_CONFIG $sdl_config_args --version | \ + sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'` + if test "x$enable_sdltest" = "xyes" ; then + ac_save_CFLAGS="$CFLAGS" + ac_save_CXXFLAGS="$CXXFLAGS" + ac_save_LIBS="$LIBS" + CFLAGS="$CFLAGS $SDL_CFLAGS" + CXXFLAGS="$CXXFLAGS $SDL_CFLAGS" + LIBS="$LIBS $SDL_LIBS" dnl dnl Now check if the installed SDL is sufficiently new. (Also sanity -dnl checks the results of sdl-config to some extent +dnl checks the results of sdl2-config to some extent dnl rm -f conf.sdltest AC_TRY_RUN([ @@ -114,32 +127,36 @@ int main (int argc, char *argv[]) } else { - printf("\n*** 'sdl-config --version' returned %d.%d.%d, but the minimum version\n", $sdl_major_version, $sdl_minor_version, $sdl_micro_version); - printf("*** of SDL required is %d.%d.%d. If sdl-config is correct, then it is\n", major, minor, micro); + printf("\n*** 'sdl2-config --version' returned %d.%d.%d, but the minimum version\n", $sdl_major_version, $sdl_minor_version, $sdl_micro_version); + printf("*** of SDL required is %d.%d.%d. If sdl2-config is correct, then it is\n", major, minor, micro); printf("*** best to upgrade to the required version.\n"); - printf("*** If sdl-config was wrong, set the environment variable SDL_CONFIG\n"); - printf("*** to point to the correct copy of sdl-config, and remove the file\n"); + printf("*** If sdl2-config was wrong, set the environment variable SDL2_CONFIG\n"); + printf("*** to point to the correct copy of sdl2-config, and remove the file\n"); printf("*** config.cache before re-running configure\n"); return 1; } } ],, no_sdl=yes,[echo $ac_n "cross compiling; assumed OK... $ac_c"]) - CFLAGS="$ac_save_CFLAGS" - CXXFLAGS="$ac_save_CXXFLAGS" - LIBS="$ac_save_LIBS" - fi + CFLAGS="$ac_save_CFLAGS" + CXXFLAGS="$ac_save_CXXFLAGS" + LIBS="$ac_save_LIBS" + fi + fi + if test "x$no_sdl" = x ; then + AC_MSG_RESULT(yes) + else + AC_MSG_RESULT(no) + fi fi if test "x$no_sdl" = x ; then - AC_MSG_RESULT(yes) - ifelse([$2], , :, [$2]) + ifelse([$2], , :, [$2]) else - AC_MSG_RESULT(no) - if test "$SDL_CONFIG" = "no" ; then - echo "*** The sdl-config script installed by SDL could not be found" + if test "$SDL2_CONFIG" = "no" ; then + echo "*** The sdl2-config script installed by SDL could not be found" echo "*** If SDL was installed in PREFIX, make sure PREFIX/bin is in" - echo "*** your path, or set the SDL_CONFIG environment variable to the" - echo "*** full path to sdl-config." + echo "*** your path, or set the SDL2_CONFIG environment variable to the" + echo "*** full path to sdl2-config." else if test -f conf.sdltest ; then : @@ -169,7 +186,7 @@ int main(int argc, char *argv[]) [ echo "*** The test program failed to compile or link. See the file config.log for the" echo "*** exact error that occured. This usually means SDL was incorrectly installed" echo "*** or that you have moved SDL since it was installed. In the latter case, you" - echo "*** may want to edit the sdl-config script: $SDL_CONFIG" ]) + echo "*** may want to edit the sdl2-config script: $SDL2_CONFIG" ]) CFLAGS="$ac_save_CFLAGS" CXXFLAGS="$ac_save_CXXFLAGS" LIBS="$ac_save_LIBS" @@ -182,4 +199,4 @@ int main(int argc, char *argv[]) AC_SUBST(SDL_CFLAGS) AC_SUBST(SDL_LIBS) rm -f conf.sdltest -]) \ No newline at end of file +]) diff --git a/chlstate.cpp b/chlstate.cpp deleted file mode 100644 index 5c9f488..0000000 --- a/chlstate.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "includes.h" - -ChooseHighscoreListState::ChooseHighscoreListState() -{ - background = SDL_LoadBMP("resources/chl.bmp"); - pointer = SDL_LoadBMP("resources/pointer.bmp"); - - selection = 0; -} - -void ChooseHighscoreListState::input(SDL_keysym key) -{ - if ((key.sym == SDLK_UP) && (selection != 0)) - { - selection--; - } else if ((key.sym == SDLK_DOWN) && (selection != 2)) - { - selection++; - } else if (key.sym == SDLK_RETURN) - { - switch (selection) - { - case 0: - changeState(new LocalHighscoreListState(false)); - - break; - case 1: - changeState(new GlobalHighscoreListState()); - - break; - case 2: - changeState(new TitleState()); - - break; - } - } -} - -void ChooseHighscoreListState::render(SDL_Surface* screen) -{ - SDL_BlitSurface(background, NULL, screen, NULL); - - SDL_Rect pSpace; - pSpace.x = 127; - pSpace.y = (selection==0?306:(selection==1?336:396)); - pSpace.w = pointer->w; - pSpace.h = pointer->h; - - SDL_BlitSurface(pointer, NULL, screen, &pSpace); -} diff --git a/chlstate.h b/chlstate.h deleted file mode 100644 index 15f1d05..0000000 --- a/chlstate.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef CHLSTATE_H -#define CHLSTATE_H - -class ChooseHighscoreListState : public State { - public: - ChooseHighscoreListState(); - void input(SDL_keysym key); - void render(SDL_Surface* screen); - - private: - SDL_Surface* background; - SDL_Surface* pointer; - int selection; -}; - -#endif diff --git a/configure.ac b/configure.ac index f311aa6..4206e6b 100644 --- a/configure.ac +++ b/configure.ac @@ -1,44 +1,44 @@ -AC_INIT(mazeoflife, version-2.0) +AC_INIT(mazeoflife, version-3.0) AC_PREREQ([2.59]) AM_INIT_AUTOMAKE([1.10 no-define foreign]) AC_PROG_CXX AC_CONFIG_FILES([Makefile]) dnl Check for SDL -SDL_VERSION=1.2.0 -AM_PATH_SDL($SDL_VERSION, +SDL_VERSION=2.0.0 +AM_PATH_SDL2($SDL_VERSION, :, AC_MSG_ERROR([*** SDL version $SDL_VERSION not found!]) ) CXXFLAGS="$CXXFLAGS $SDL_CFLAGS" LIBS="$LIBS $SDL_LIBS" -dnl Check for SDL_ttf -AC_ARG_WITH([sdl-ttf-include-path], - [AS_HELP_STRING([--with-sdl-ttf-include-path], - [location of the SDL_ttf headers, defaults to /usr/include/SDL])], +dnl Check for SDL2_ttf +AC_ARG_WITH([sdl-ttf2-include-path], + [AS_HELP_STRING([--with-sdl2-ttf-include-path], + [location of the SDL2_ttf headers, defaults to /usr/include/SDL2])], [SDLTTF_CXXFLAGS="-I$withval"], - [SDLTTF_CXXFLAGS="-I/usr/include/SDL"]) + [SDLTTF_CXXFLAGS="-I/usr/include/SDL2"]) AC_SUBST([SDLTTF_CXXFLAGS]) -AC_ARG_WITH([sdl-ttf-lib-path], - [AS_HELP_STRING([--with-sdl-ttf-lib-path], [location of the SDL_ttf library])], - [SDLTTF_LIBS="-L$withval -lsdl_ttf"], - [SDLTTF_LIBS="-lsdl_ttf"]) +AC_ARG_WITH([sdl2-ttf-lib-path], + [AS_HELP_STRING([--with-sdl2-ttf-lib-path], [location of the SDL2_ttf library])], + [SDLTTF_LIBS="-L$withval -lsdl2_ttf"], + [SDLTTF_LIBS="-lsdl2_ttf"]) AC_SUBST([SDLTTF_LIBS]) -dnl Check for SDL_net -AC_ARG_WITH([sdl-net-include-path], - [AS_HELP_STRING([--with-sdl-net-include-path], - [location of the SDL_net headers, defaults to /usr/include/SDL])], +dnl Check for SDL2_net +AC_ARG_WITH([sdl2-net-include-path], + [AS_HELP_STRING([--with-sdl2-net-include-path], + [location of the SDL2_net headers, defaults to /usr/include/SDL2])], [SDLNET_CXXFLAGS="-I$withval"], - [SDLNET_CXXFLAGS="-I/usr/include/SDL"]) + [SDLNET_CXXFLAGS="-I/usr/include/SDL2"]) AC_SUBST([SDLNET_CXXFLAGS]) -AC_ARG_WITH([sdl-net-lib-path], - [AS_HELP_STRING([--with-sdl-net-lib-path], [location of the SDL_net library])], - [SDLNET_LIBS="-L$withval -lsdl_net"], - [SDLNET_LIBS="-lsdl_net"]) +AC_ARG_WITH([sdl2-net-lib-path], + [AS_HELP_STRING([--with-sdl2-net-lib-path], [location of the SDL2_net library])], + [SDLNET_LIBS="-L$withval -lsdl2_net"], + [SDLNET_LIBS="-lsdl2_net"]) AC_SUBST([SDLNET_LIBS]) AC_OUTPUT \ No newline at end of file diff --git a/gamestate.cpp b/gamestate.cpp deleted file mode 100644 index fcf865f..0000000 --- a/gamestate.cpp +++ /dev/null @@ -1,317 +0,0 @@ -#include "includes.h" - -GameState::GameState() -{ - player_color = getColor(255, 255, 0); - event_color = getColor(0, 0, 255); - - newGame = false; - - info = new Info(); - info->playerx = 1; - info->playery = 1; - info->level = Level(); - info->doneMaking = false; - board = Board(info); -} - -void GameState::input(SDL_keysym key) -{ - if (info->doneMaking) - { - switch (key.sym) - { - case SDLK_LEFT: - move(info->playerx-1, info->playery); - - break; - case SDLK_RIGHT: - move(info->playerx+1, info->playery); - - break; - case SDLK_UP: - move(info->playerx, info->playery-1); - - break; - case SDLK_DOWN: - move(info->playerx, info->playery+1); - - break; - case SDLK_ESCAPE: - std::ifstream exists(getDataFile()); - if (exists) - { - FILE* hslist = fopen(getDataFile(), "r"); - int scores; - Highscore* h; - - fscanf(hslist, "%d%*c", &scores); - - if (scores < 10) - { - fclose(hslist); - - changeState(new NewHighscoreState(info->level.getLevel())); - } else { - for (int i=0; igetLevel() < info->level.getLevel()) - { - changeState(new NewHighscoreState(info->level.getLevel())); - } else { - changeState(new LocalHighscoreListState(true)); - } - } - } else { - changeState(new NewHighscoreState(info->level.getLevel())); - } - - break; - } - } -} - -void GameState::tick() -{ - if (newGame) - { - switch (rand()%4) - { - case 0: info->playerx = 1; info->playery = 1; break; - case 1: info->playerx = 1; info->playery = HEIGHT-2; break; - case 2: info->playerx = WIDTH-2; info->playery = HEIGHT-2; break; - case 3: info->playerx = WIDTH-2; info->playery = 1; break; - } - - info->level.incrementLevel(); - info->doneMaking = false; - info->gens = 0; - board = Board(info); - newGame = false; - } - - board.tick(); -} - -void GameState::move(int x, int y) -{ - wrap(&x, &y); - - if (board.isObstructed(x,y)) return; - if ((x==15)&&(y==15)) newGame = true; - - info->playerx = x; - info->playery = y; -} - -void GameState::render(SDL_Surface* screen) -{ - // Paint maze - board.render(screen); - - // Paint event - SDL_Rect block; - block.x = 15*16; - block.y = 15*16; - block.w = 16; - block.h = 16; - - SDL_FillRect(screen, &block, event_color); - - if (!info->doneMaking) - { - SDL_Color fontColor = {0, 0, 0, 0}; - char levelnum[8]; - sprintf(levelnum, "%d", info->level.getLevel()); - SDL_Surface* title = TTF_RenderText_Solid(loadFont(100), levelnum, fontColor); - SDL_Rect tSpace = {240-(title->w/2), 240-(title->h/2), title->w, title->h}; - SDL_FillRect(screen, NULL, info->level.getDeadColor()); - SDL_BlitSurface(title, NULL, screen, &tSpace); - } - - // Paint player - block.x = info->playerx*16; - block.y = info->playery*16; - - SDL_FillRect(screen, &block, player_color); -} - -GameState::Level::Level() -{ - level = 0; - - alive[0] = getColor(0, 0, 0); // Black - alive[1] = getColor(255, 0, 0); // Red - alive[2] = getColor(0, 255, 0); // Green - alive[3] = getColor(85, 85, 85); // Dark Gray - alive[4] = getColor(255, 0, 255); // Magenta - - dead[0] = getColor(255, 255, 255); // White - dead[1] = getColor(255, 192, 203); // Pink - dead[2] = getColor(0, 255, 255); // Cyan - dead[3] = getColor(170, 170, 170); // Light Gray - dead[4] = getColor(255, 128, 0); // Orange -} - -int GameState::Level::getLevel() -{ - return level; -} - -bool GameState::Level::checkSquare(int x, int y) -{ - switch (level/10+1) - { - case 1: - return ((x>13)&&(x<17)&&(y>13)&&(y<17)); - case 2: - case 3: - return ((x>12)&&(x<18)&&(y>12)&&(y<18)); - case 4: - case 5: - return ((x>11)&&(x<19)&&(y>11)&&(y<19)); - default: - return true; - } -} - -Uint32 GameState::Level::getAliveColor() -{ - return alive[(level/10)%5]; -} - -Uint32 GameState::Level::getDeadColor() -{ - return dead[(level/10)%5]; -} - -void GameState::Level::incrementLevel() -{ - level++; -} - -GameState::Board::Board() -{ - GameState::Board::Board(new GameState::Info()); -} - -GameState::Board::Board(GameState::Info* info) -{ - this->info = info; - - int x,y; - for (y=0;ylevel.checkSquare(x, y)) - { - blocks[x][y] = rand() % 2; - } else { - blocks[x][y] = false; - } - } - } - - blocks[15][15] = false; -} - -bool GameState::Board::isObstructed(int x, int y) -{ - return blocks[x][y]; -} - -void GameState::Board::render(SDL_Surface* screen) -{ - SDL_Rect block; - block.w = 16; - block.h = 16; - - int x,y; - - for (y=0;ylevel.getAliveColor() : info->level.getDeadColor())); - } - } -} - -void GameState::Board::tick() -{ - bool temp[WIDTH][HEIGHT]; - int x,y; - for (x=0;x= 1) && (neighbors <= 4)); - } else { - blocks[x][y] = (neighbors == 3); - } - } - } - - if (!info->doneMaking && ++info->gens > 50) info->doneMaking = true; -} - -void GameState::Board::incrementIfNeighbor(int x, int y, bool temp[WIDTH][HEIGHT], int* tick) -{ - int nx = x; - int ny = y; - - wrap(&x, &y); - - if (!((nx!=x)&&(ny!=y))) - { - if ((temp[x][y])||((info->playerx==x)&&(info->playery==y))||((x==15)&&(y==15))) - { - ++*tick; - } - } -} diff --git a/gamestate.h b/gamestate.h deleted file mode 100644 index 28b85c0..0000000 --- a/gamestate.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef GAMESTATE_H -#define GAMESTATE_H - -class GameState : public State { - public: - GameState(); - void input(SDL_keysym key); - void tick(); - void render(SDL_Surface* screen); - - class Level - { - private: - int level; - Uint32 alive[5]; - Uint32 dead[5]; - - public: - Level(); - int getLevel(); - int getLevelGroup(); - bool checkSquare(int x, int y); - Uint32 getAliveColor(); - Uint32 getDeadColor(); - void incrementLevel(); - }; - - struct Info { - int playerx, playery; - Level level; - bool doneMaking; - int gens; - }; - - class Board - { - private: - bool blocks[WIDTH][HEIGHT]; - void incrementIfNeighbor(int x, int y, bool temp[WIDTH][HEIGHT], int* tick); - GameState::Info* info; - - public: - Board(); - Board(GameState::Info* info); - bool isObstructed(int x, int y); - void render(SDL_Surface* screen); - void tick(); - }; - - private: - Uint32 player_color; - Uint32 event_color; - bool newGame; - Info* info; - Board board; - void move(int x, int y); -}; - -#endif diff --git a/highscore.cpp b/highscore.cpp index 6811fa8..7133e1a 100644 --- a/highscore.cpp +++ b/highscore.cpp @@ -1,4 +1,4 @@ -#include "includes.h" +#include "highscore.h" Highscore::Highscore(char* name, int level) { diff --git a/hsglobal.cpp b/hsglobal.cpp deleted file mode 100644 index 3076403..0000000 --- a/hsglobal.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "includes.h" - -GlobalHighscoreListState::GlobalHighscoreListState() -{ - options = SDL_LoadBMP("resources/hlo_rtm.bmp"); - pointer = SDL_LoadBMP("resources/pointer.bmp"); - - list = SDL_CreateRGBSurface(SDL_SWSURFACE || SDL_SRCCOLORKEY, 480, 480, 32, 0,0,0,0); - Uint32 bgColor = SDL_MapRGB(list->format, 255, 255, 255); - SDL_FillRect(list, NULL, bgColor); - SDL_SetColorKey(list, SDL_SRCCOLORKEY, bgColor); - TTF_Font* dataFont = loadFont(25); - SDL_Color fontColor = {0, 0, 0, 0}; - SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Fetching highscores....", fontColor); - SDL_Rect aSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h}; - SDL_BlitSurface(text, NULL, list, &aSpace); - - SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); - SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; - SDL_BlitSurface(title, NULL, list, &tSpace); - - SDL_Rect oSpace = {0, 440, options->w, options->h}; - SDL_BlitSurface(options, NULL, list, &oSpace); - - SDL_CreateThread(&LoadHighscoreList, this); -} - -int GlobalHighscoreListState::LoadHighscoreList(void* pParam) -{ - GlobalHighscoreList* lhl = new GlobalHighscoreList(); - ((GlobalHighscoreListState*)pParam)->list = lhl->render(); - - SDL_Color fontColor = {0, 0, 0, 0}; - SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); - SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; - SDL_BlitSurface(title, NULL, ((GlobalHighscoreListState*)pParam)->list, &tSpace); - - SDL_Rect oSpace = {0, 440, ((GlobalHighscoreListState*)pParam)->options->w, ((GlobalHighscoreListState*)pParam)->options->h}; - SDL_BlitSurface(((GlobalHighscoreListState*)pParam)->options, NULL, ((GlobalHighscoreListState*)pParam)->list, &oSpace); -} - -void GlobalHighscoreListState::input(SDL_keysym key) -{ - if (key.sym == SDLK_RETURN) - { - changeState(new ChooseHighscoreListState()); - } -} - -void GlobalHighscoreListState::render(SDL_Surface* screen) -{ - SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255)); - - SDL_BlitSurface(list, NULL, screen, NULL); - - SDL_Rect pSpace; - pSpace.x = 137; - pSpace.y = 449; - pSpace.w = pointer->w; - pSpace.h = pointer->h; - - SDL_BlitSurface(pointer, NULL, screen, &pSpace); -} diff --git a/hsglobal.h b/hsglobal.h deleted file mode 100644 index 5b92b0e..0000000 --- a/hsglobal.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef HSGLOBAL_H -#define HSGLOBAL_H - -class GlobalHighscoreListState : public State { - public: - GlobalHighscoreListState(); - void input(SDL_keysym key); - void render(SDL_Surface* screen); - - private: - static int LoadHighscoreList(void* pParam); - - SDL_Surface* list; - SDL_Surface* options; - SDL_Surface* pointer; -}; - -#endif diff --git a/hslist.cpp b/hslist.cpp index ec99a77..80e0801 100644 --- a/hslist.cpp +++ b/hslist.cpp @@ -1,11 +1,16 @@ -#include "includes.h" +#include "hslist.h" +#include +#include +#include +#include +#include "util.h" SDL_Surface* HighscoreList::render() { - SDL_Surface* tmp = SDL_CreateRGBSurface(SDL_SWSURFACE || SDL_SRCCOLORKEY, 480, 480, 32, 0,0,0,0); + SDL_Surface* tmp = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0); Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255); SDL_FillRect(tmp, NULL, bgColor); - SDL_SetColorKey(tmp, SDL_SRCCOLORKEY, bgColor); + SDL_SetColorKey(tmp, SDL_TRUE, bgColor); TTF_Font* posFont = loadFont(40); TTF_Font* dataFont = loadFont(25); SDL_Color fontColor = {0, 0, 0, 0}; @@ -181,10 +186,10 @@ SDL_Surface* GlobalHighscoreList::render() { if (fail) { - SDL_Surface* tmp = SDL_CreateRGBSurface(SDL_SWSURFACE || SDL_SRCCOLORKEY, 480, 480, 32, 0,0,0,0); + SDL_Surface* tmp = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0); Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255); SDL_FillRect(tmp, NULL, bgColor); - SDL_SetColorKey(tmp, SDL_SRCCOLORKEY, bgColor); + SDL_SetColorKey(tmp, SDL_TRUE, bgColor); TTF_Font* dataFont = loadFont(25); SDL_Color fontColor = {0, 0, 0, 0}; SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Error retrieving highscores", fontColor); diff --git a/hslist.h b/hslist.h index bebb85a..80e1c31 100644 --- a/hslist.h +++ b/hslist.h @@ -1,3 +1,7 @@ +#include +#include +#include "highscore.h" + #ifndef HSLIST_H #define HSLIST_H diff --git a/hslocal.cpp b/hslocal.cpp deleted file mode 100644 index eec87e2..0000000 --- a/hslocal.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include "includes.h" - -LocalHighscoreListState::LocalHighscoreListState(bool fromGame) -{ - this->fromGame = fromGame; - - if (fromGame) - { - options = SDL_LoadBMP("resources/hlo_paartm.bmp"); - } else { - options = SDL_LoadBMP("resources/hlo_rtm.bmp"); - } - - pointer = SDL_LoadBMP("resources/pointer.bmp"); - - LocalHighscoreList* lhl = new LocalHighscoreList(); - list = lhl->render(); - - SDL_Color fontColor = {0, 0, 0, 0}; - SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); - SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; - SDL_BlitSurface(title, NULL, list, &tSpace); - - SDL_Rect oSpace = {0, 440, options->w, options->h}; - SDL_BlitSurface(options, NULL, list, &oSpace); - - selection = 0; -} - -void LocalHighscoreListState::input(SDL_keysym key) -{ - if (fromGame) - { - if ((key.sym == SDLK_LEFT) && (selection != 0)) - { - selection--; - } else if ((key.sym == SDLK_RIGHT) && (selection != 1)) - { - selection++; - } else if (key.sym == SDLK_RETURN) - { - switch (selection) - { - case 0: - changeState(new GameState()); - - break; - case 1: - changeState(new TitleState()); - - break; - } - } - } else { - if (key.sym == SDLK_RETURN) - { - changeState(new ChooseHighscoreListState()); - } - } -} - -void LocalHighscoreListState::render(SDL_Surface* screen) -{ - SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255)); - - SDL_BlitSurface(list, NULL, screen, NULL); - - SDL_Rect pSpace; - pSpace.w = pointer->w; - pSpace.h = pointer->h; - - if (fromGame) - { - pSpace.x = (selection==0?52:225); - pSpace.y = 447; - } else { - pSpace.x = 137; - pSpace.y = 449; - } - - SDL_BlitSurface(pointer, NULL, screen, &pSpace); -} diff --git a/hslocal.h b/hslocal.h deleted file mode 100644 index 8708d03..0000000 --- a/hslocal.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef HSLOCAL_H -#define HSLOCAL_H - -class LocalHighscoreListState : public State { - public: - LocalHighscoreListState(bool fromGame); - void input(SDL_keysym key); - void render(SDL_Surface* screen); - - private: - SDL_Surface* list; - SDL_Surface* options; - SDL_Surface* pointer; - int selection; - bool fromGame; -}; - -#endif diff --git a/hsnew.cpp b/hsnew.cpp deleted file mode 100644 index b4894c2..0000000 --- a/hsnew.cpp +++ /dev/null @@ -1,220 +0,0 @@ -#include "includes.h" - -NewHighscoreState::NewHighscoreState(int level) -{ - this->level = level; - - options = SDL_LoadBMP("resources/hlo_passartm.bmp"); - pointer = SDL_LoadBMP("resources/pointer.bmp"); - - lhl = new NewHighscoreList(level); - newpos = lhl->getNewPos(); - list = lhl->render(); - - SDL_Color fontColor = {0, 0, 0, 0}; - SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "New Highscore!", fontColor); - SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; - SDL_BlitSurface(title, NULL, list, &tSpace); - - this->enterName = true; - this->lp = 0; - this->hsname = (char*) calloc(25, sizeof(char)); - - SDL_Surface* text = TTF_RenderText_Blended(loadFont(25), "Enter Your Name", fontColor); - SDL_Rect oSpace = {240-(text->w/2), 440, text->w, text->h}; - SDL_BlitSurface(text, NULL, list, &oSpace); - - selection = 0; - - int posw, posh; - char pos[3]; // 2 max characters in rank plus the colon at the end - sprintf(pos, "%d:", newpos); - char name[26]; // 25 max characters in username plus the space at the beginning - sprintf(name, " %s", hsname); - newName = TTF_RenderText_Blended(loadFont(25), name, fontColor); - TTF_SizeText(loadFont(40), pos, &posw, &posh); - rntSpace.x = posw; - rntSpace.y = newpos*40+((posh/2)-(newName->h/2)); - rntSpace.w = newName->w; - rntSpace.h = newName->h; - - SDL_EnableUNICODE(1); -} - -void NewHighscoreState::input(SDL_keysym key) -{ - if (enterName) - { - if ((key.sym == SDLK_BACKSPACE) && (lp > 0)) - { - hsname[--lp] = 0; - - SDL_Color fontColor = {0, 0, 0, 0}; - char name[26]; // 25 max characters in username plus the space at the beginning - sprintf(name, " %s", hsname); - newName = TTF_RenderText_Blended(loadFont(25), name, fontColor); - rntSpace.w = newName->w; - rntSpace.h = newName->h; - } else if ((key.sym == SDLK_RETURN) && (hsname[0] != 0)) - { - SDL_EnableUNICODE(0); - enterName = false; - - lhl->addToList(hsname); - LocalHighscoreList* lhl2 = new LocalHighscoreList(); - list = lhl2->render(); - - SDL_Color fontColor = {0, 0, 0, 0}; - SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "New Highscore!", fontColor); - SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; - SDL_BlitSurface(title, NULL, list, &tSpace); - - SDL_Rect oSpace = {0, 440, options->w, options->h}; - SDL_BlitSurface(options, NULL, list, &oSpace); - } else if (((key.unicode & 0xFF80) == 0) && (key.unicode >= 32 && key.unicode < 127) && (lp < 25)) - { - hsname[lp++] = key.unicode & 0x7f; - hsname[lp] = 0; - - SDL_Color fontColor = {0, 0, 0, 0}; - char name[26]; // 25 max characters in username plus the space at the beginning - sprintf(name, " %s", hsname); - newName = TTF_RenderText_Blended(loadFont(25), name, fontColor); - rntSpace.w = newName->w; - rntSpace.h = newName->h; - } - } else { - if ((key.sym == SDLK_LEFT) && (selection != 0)) - { - selection--; - } else if ((key.sym == SDLK_RIGHT) && (selection != 2)) - { - selection++; - } else if (key.sym == SDLK_RETURN) - { - switch (selection) - { - case 0: - changeState(new GameState()); - - break; - case 1: - changeState(new SubmitHighscoreListState(hsname, level)); - - break; - case 2: - changeState(new TitleState()); - - break; - } - } - } -} - -void NewHighscoreState::render(SDL_Surface* screen) -{ - SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255)); - - SDL_Rect eSpace = {0, newpos*40, 480, 40}; - SDL_FillRect(screen, &eSpace, SDL_MapRGB(screen->format, 255, 255, 0)); - - SDL_BlitSurface(list, NULL, screen, NULL); - - if (enterName) - { - SDL_BlitSurface(newName, NULL, screen, &rntSpace); - } else { - SDL_Rect pSpace; - pSpace.x = (selection==0?13:(selection==1?138:284)); - pSpace.y = 448; - pSpace.w = pointer->w; - pSpace.h = pointer->h; - - SDL_BlitSurface(pointer, NULL, screen, &pSpace); - } -} - -NewHighscoreState::NewHighscoreList::NewHighscoreList(int level) -{ - this->hslist = getLocalHighscores(); - - if (this->hslist.empty()) - { - Highscore n = Highscore("", level); - n.setRank(1); - - this->hslist.push_back(n); - this->newpos = n.getRank(); - } else { - std::vector::iterator it; - bool found = false; - int lastrank; - int i=0; - - for (it=this->hslist.begin(); ithslist.end(); it++) - { - Highscore h = *it; - lastrank = h.getRank(); - - if (!found) - { - if (h.getLevel() < level) - { - Highscore n = Highscore("", level); - n.setRank(h.getRank()); - - this->hslist.insert(it, n); - this->newpos = n.getRank(); - - if (this->hslist.size() > 10) - { - this->hslist.pop_back(); - } - - found = true; - } - } else { - //this->hslist[i].setRank(h.getRank()+1); - } - - i++; - } - - if (!found) - { - Highscore n = Highscore("", level); - n.setRank(lastrank+1); - - this->hslist.push_back(n); - this->newpos = n.getRank(); - } - } -} - -int NewHighscoreState::NewHighscoreList::getNewPos() -{ - return newpos; -} - -void NewHighscoreState::NewHighscoreList::addToList(char* name) -{ - FILE* hsfile = fopen(getDataFile(), "w"); - fprintf(hsfile, "%d ", this->hslist.size()); - - std::vector::iterator it; - - for (it=this->hslist.begin(); ithslist.end(); it++) - { - Highscore h = *it; - - if (h.getName() == "") - { - h = Highscore(name, h.getLevel()); - h.setRank(newpos); - } - - fprintf(hsfile, "%d%s%d ", strlen(h.getName()), h.getName(), h.getLevel()); - } - - fclose(hsfile); -} diff --git a/hsnew.h b/hsnew.h deleted file mode 100644 index 123ac53..0000000 --- a/hsnew.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef HSNEW_H -#define HSNEW_H - -class NewHighscoreState : public State { - public: - NewHighscoreState(int level); - void input(SDL_keysym key); - void render(SDL_Surface* screen); - - private: - class NewHighscoreList : public HighscoreList { - public: - NewHighscoreList(int level); - int getNewPos(); - void addToList(char* name); - - private: - int newpos; - }; - - SDL_Surface* list; - SDL_Surface* options; - SDL_Surface* pointer; - int selection; - int level; - int newpos; - int lp; - char* hsname; - bool enterName; - SDL_Rect rntSpace; - SDL_Surface* newName; - NewHighscoreList* lhl; -}; - -#endif diff --git a/hssubmit.cpp b/hssubmit.cpp deleted file mode 100644 index eaacb88..0000000 --- a/hssubmit.cpp +++ /dev/null @@ -1,226 +0,0 @@ -#include "includes.h" - -SubmitHighscoreListState::SubmitHighscoreListState(char* hsname, int level) -{ - pointer = SDL_LoadBMP("resources/pointer.bmp"); - - this->hsname = hsname; - this->level = level; - this->scoreSent = false; - this->selection = 0; - - list = SDL_CreateRGBSurface(SDL_SWSURFACE || SDL_SRCCOLORKEY, 480, 480, 32, 0,0,0,0); - Uint32 bgColor = SDL_MapRGB(list->format, 255, 255, 255); - SDL_FillRect(list, NULL, bgColor); - SDL_SetColorKey(list, SDL_SRCCOLORKEY, bgColor); - TTF_Font* dataFont = loadFont(25); - SDL_Color fontColor = {0, 0, 0, 0}; - SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Sending highscore....", fontColor); - SDL_Rect aSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h}; - SDL_BlitSurface(text, NULL, list, &aSpace); - - SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); - SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; - SDL_BlitSurface(title, NULL, list, &tSpace); - - SDL_CreateThread(&LoadHighscoreList, this); -} - -int SubmitHighscoreListState::LoadHighscoreList(void* pParam) -{ - SubmitHighscoreList* lhl = new SubmitHighscoreList(((SubmitHighscoreListState*)pParam)->hsname, ((SubmitHighscoreListState*)pParam)->level); - ((SubmitHighscoreListState*)pParam)->list = lhl->render(); - - SDL_Color fontColor = {0, 0, 0, 0}; - SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); - SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; - SDL_BlitSurface(title, NULL, ((SubmitHighscoreListState*)pParam)->list, &tSpace); - - if (lhl->hasFailed()) - { - ((SubmitHighscoreListState*)pParam)->options = SDL_LoadBMP("resources/hlo_passartm.bmp"); - } else { - ((SubmitHighscoreListState*)pParam)->options = SDL_LoadBMP("resources/hlo_paartm.bmp"); - } - - SDL_Rect oSpace = {0, 440, ((SubmitHighscoreListState*)pParam)->options->w, ((SubmitHighscoreListState*)pParam)->options->h}; - SDL_BlitSurface(((SubmitHighscoreListState*)pParam)->options, NULL, ((SubmitHighscoreListState*)pParam)->list, &oSpace); - - ((SubmitHighscoreListState*)pParam)->fail = lhl->hasFailed(); - ((SubmitHighscoreListState*)pParam)->newpos = lhl->getNewPos(); - ((SubmitHighscoreListState*)pParam)->scoreSent = true; -} - -void SubmitHighscoreListState::input(SDL_keysym key) -{ - if (scoreSent) - { - if ((key.sym == SDLK_LEFT) && (selection != 0)) - { - selection--; - } else if ((key.sym == SDLK_RIGHT) && (selection != (fail?2:1))) - { - selection++; - } else if (key.sym == SDLK_RETURN) - { - if (fail) - { - switch (selection) - { - case 0: - changeState(new GameState()); - - break; - case 1: - changeState(new SubmitHighscoreListState(hsname, level)); - - break; - case 2: - changeState(new TitleState()); - - break; - } - } else { - switch (selection) - { - case 0: - changeState(new GameState()); - - break; - case 1: - changeState(new TitleState()); - - break; - } - } - } - } -} - -void SubmitHighscoreListState::render(SDL_Surface* screen) -{ - SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255)); - - if (scoreSent) - { - SDL_Rect pSpace; - if (fail) - { - pSpace.x = (selection==0?13:(selection==1?138:284)); - pSpace.y = 448; - } else { - pSpace.x = (selection==0?52:225); - pSpace.y = 447; - } - pSpace.w = pointer->w; - pSpace.h = pointer->h; - - SDL_BlitSurface(pointer, NULL, screen, &pSpace); - - SDL_Rect eSpace = {0, newpos*40, 480, 40}; - SDL_FillRect(screen, &eSpace, SDL_MapRGB(screen->format, 255, 255, 0)); - } - - SDL_BlitSurface(list, NULL, screen, NULL); -} - -SubmitHighscoreListState::SubmitHighscoreList::SubmitHighscoreList(char* hsname, int level) -{ - fail = false; - - try - { - IPaddress ipaddress; - - if (SDLNet_ResolveHost(&ipaddress, "other.fourisland.com", 80) == -1) - { - printf("Could not resolve host \"other.fourisland.com\": %s\n", SDLNet_GetError()); - throw 1; - } - - TCPsocket tcpsock = SDLNet_TCP_Open(&ipaddress); - if (!tcpsock) - { - printf("Could not connect to host \"other.fourisland.com\": %s\n", SDLNet_GetError()); - throw 2; - } - - char body[256]; - sprintf(body, "name=%s&level=%d", hsname, level); - char headers[256]; - sprintf(headers, "POST /mol/hslist.php?add HTTP/1.1\nHost: other.fourisland.com\nUser-Agent: Maze Of Life v2.0\nAccept: text/plain\nKeep-Alive: 300\nConnection: keep-alive\nContent-Type: application/x-www-form-urlencoded\nContent-Length: %d\n\n%s\n", strlen(body), body); - if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers)+1) < strlen(headers)) - { - printf("Connection closed by peer: %s\n", SDLNet_GetError()); - throw 3; - } - - std::stringstream download(std::stringstream::in | std::stringstream::out); - char hslist[1024]; - SDLNet_TCP_Recv(tcpsock, hslist, 1024); - download << hslist; - SDLNet_TCP_Close(tcpsock); - - char temps[256]; - download.getline(temps,256); - while (strlen(temps) != 1) - { - download.getline(temps,256); - } - - int rank; - download.getline(temps, 256); - if (sscanf(temps, "%d%*c", &rank) != 1) - { - printf("Recieved data is of an invalid format: %s\n", temps); - throw 4; - } - - this->hslist = getGlobalHighscores(); - - if (this->hslist.empty()) - { - printf("Global Highscore List cannot be empty after adding a score to it.\n"); - throw 5; - } - - if (rank > 10) - { - Highscore temph(hsname, level); - temph.setRank(rank); - - this->hslist[9] = temph; - this->newpos = 10; - } else { - std::vector::iterator it; - bool found = false; - - for (it=this->hslist.begin(); ithslist.end(); it++) - { - Highscore h = *it; - - if (!found) - { - if ((strcmp(h.getName(),hsname) == 0) && (h.getLevel() == level)) - { - this->newpos = h.getRank(); - found = true; - } - } - } - } - } catch (int e) - { - fail = true; - } -} - -int SubmitHighscoreListState::SubmitHighscoreList::getNewPos() -{ - return newpos; -} - -bool SubmitHighscoreListState::SubmitHighscoreList::hasFailed() -{ - return fail; -} diff --git a/hssubmit.h b/hssubmit.h deleted file mode 100644 index 243dab8..0000000 --- a/hssubmit.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef HSSUBMIT_H -#define HSSUBMIT_H - -class SubmitHighscoreListState : public State { - public: - SubmitHighscoreListState(char* hsname, int level); - void input(SDL_keysym key); - void render(SDL_Surface* screen); - - private: - static int LoadHighscoreList(void* pParam); - - class SubmitHighscoreList : public GlobalHighscoreList { - public: - SubmitHighscoreList(char* hsname, int level); - int getNewPos(); - bool hasFailed(); - - private: - int newpos; - }; - - SDL_Surface* list; - SDL_Surface* options; - SDL_Surface* pointer; - char* hsname; - int level; - bool scoreSent; - int selection; - int newpos; - bool fail; -}; - -#endif diff --git a/htpstate.cpp b/htpstate.cpp deleted file mode 100644 index 0771290..0000000 --- a/htpstate.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "includes.h" - -HowToPlayState::HowToPlayState() -{ - background1 = SDL_LoadBMP("resources/htp1.bmp"); - background2 = SDL_LoadBMP("resources/htp2.bmp"); - pointer = SDL_LoadBMP("resources/pointer.bmp"); - - secondPage = false; - selection = 0; -} - -void HowToPlayState::input(SDL_keysym key) -{ - if ((key.sym == SDLK_LEFT) && (selection != 0)) - { - selection--; - } else if ((key.sym == SDLK_RIGHT) && (selection != 1)) - { - selection++; - } else if (key.sym == SDLK_RETURN) - { - switch (selection) - { - case 0: - secondPage = !secondPage; - - break; - case 1: - changeState(new TitleState()); - - break; - } - } -} - -void HowToPlayState::render(SDL_Surface* screen) -{ - SDL_Rect pSpace; - - if (!secondPage) - { - SDL_BlitSurface(background1, NULL, screen, NULL); - - pSpace.x = (selection==0?74:216); - } else { - SDL_BlitSurface(background2, NULL, screen, NULL); - - pSpace.x = (selection==0?45:238); - } - - pSpace.y = 430; - pSpace.w = pointer->w; - pSpace.h = pointer->h; - - SDL_BlitSurface(pointer, NULL, screen, &pSpace); -} diff --git a/htpstate.h b/htpstate.h deleted file mode 100644 index 79c66ba..0000000 --- a/htpstate.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef HTPSTATE_H -#define HTPSTATE_H - -class HowToPlayState : public State { - public: - HowToPlayState(); - void input(SDL_keysym key); - void render(SDL_Surface* screen); - - private: - SDL_Surface* background1; - SDL_Surface* background2; - SDL_Surface* pointer; - bool secondPage; - int selection; -}; - -#endif diff --git a/icon.ico b/icon.ico deleted file mode 100644 index 37e9db6..0000000 Binary files a/icon.ico and /dev/null differ diff --git a/includes.h b/includes.h deleted file mode 100644 index 38523b7..0000000 --- a/includes.h +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "state.h" -#include "mazeoflife.h" -#include "titlestate.h" -#include "htpstate.h" -#include "chlstate.h" -#include "gamestate.h" -#include "highscore.h" -#include "hslist.h" -#include "hslocal.h" -#include "hsnew.h" -#include "hsglobal.h" -#include "hssubmit.h" diff --git a/mazeoflife.cpp b/mazeoflife.cpp index 107a640..a020f6c 100644 --- a/mazeoflife.cpp +++ b/mazeoflife.cpp @@ -1,7 +1,12 @@ -#include "includes.h" - -SDL_Surface *screen; -State* state; +#include "mazeoflife.h" +#include +#include +#include +#include +#include +#include +#include "state.h" +#include "titlestate.h" int main(int argc, char *argv[]) { @@ -24,118 +29,30 @@ int main(int argc, char *argv[]) printf("Cound not initalize SDL_net: %s.\n", SDLNet_GetError()); exit(-1); } - - /* Clean up on exit */ - atexit(SDL_Quit); - atexit(TTF_Quit); - atexit(SDLNet_Quit); - - SDL_WM_SetCaption("Maze Of Life", NULL); - - SDL_Surface* icon; - icon = SDL_LoadBMP("resources/icon.bmp"); - SDL_WM_SetIcon(icon, NULL); - - /* - * Initialize the display in a 640x480 8-bit palettized mode, - * requesting a software surface - */ - screen = SDL_SetVideoMode(WIDTH*16, HEIGHT*16, 8, SDL_DOUBLEBUF); - if ( screen == NULL ) { - fprintf(stderr, "Couldn't set %dx%dx8 video mode: %s\n", WIDTH*16, WIDTH*16, SDL_GetError()); - exit(1); - } - - SDL_EnableKeyRepeat(100, 70); - - state = new TitleState(); - - SDL_AddTimer(TICKDELAY, *tick, NULL); - - SDL_Event anEvent; - for (;;) + + SDL_Window* window = SDL_CreateWindow("Maze of Life", 100, 100, 480, 480, SDL_WINDOW_SHOWN); + if (window == NULL) { - while (SDL_PollEvent(&anEvent)) - { - switch (anEvent.type) - { - case SDL_QUIT: - exit(0); - - break; - case SDL_KEYDOWN: - if (anEvent.key.keysym.sym == SDLK_F4) - { - SDL_WM_ToggleFullScreen(screen); - } else { - state->input(anEvent.key.keysym); - } - - break; - } - } + std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; + return 1; } - - exit(0); -} - -void wrap(int* x, int* y) -{ - if (*x < 0) - { - *x = WIDTH-(0-*x); - } else if (*y < 0) + + SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if (renderer == NULL) { - *y = HEIGHT-(0-*y); - } else if (*x >= WIDTH) - { - *x = *x-WIDTH; - } else if (*y >= HEIGHT) - { - *y = *y-HEIGHT; + std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; + return 1; } -} - -Uint32 getColor(int r, int g, int b) -{ - return SDL_MapRGB(screen->format, r, g, b); -} - -void changeState(State* nState) -{ - state = nState; -} - -Uint32 tick(Uint32 interval, void *param) -{ - state->tick(); - state->render(screen); - SDL_Flip(screen); - - return interval; -} - -TTF_Font* loadFont(int size) -{ - TTF_Font* tmpfont = TTF_OpenFont("resources/mono.ttf", size); - - if (tmpfont == NULL) + State* state = new TitleState(); + while (state != NULL) { - printf("Unable to load font: %s\n", TTF_GetError()); - exit(1); + state = (*state)(renderer); } - - return tmpfont; -} - -const char* getDataFile() -{ -#ifdef WINDOWS - char* dir = getenv("USERPROFILE"); -#else - char* dir = getenv("HOME"); -#endif - - return (std::string(dir) + "/.molhslist").c_str(); -} + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDLNet_Quit(); + TTF_Quit(); + SDL_Quit(); +} \ No newline at end of file diff --git a/mazeoflife.h b/mazeoflife.h index 00a31cf..3cc6d6d 100644 --- a/mazeoflife.h +++ b/mazeoflife.h @@ -3,13 +3,5 @@ const int WIDTH = 30; const int HEIGHT = 30; -const int TICKDELAY = 5; - -void wrap(int* x, int* y); -Uint32 getColor(int r, int g, int b); -void changeState(State* nState); -Uint32 tick(Uint32 interval, void *param); -TTF_Font* loadFont(int size); -const char* getDataFile(); #endif diff --git a/state.h b/state.h index 7373940..2d6f804 100644 --- a/state.h +++ b/state.h @@ -1,12 +1,12 @@ +#include + #ifndef STATE_H #define STATE_H class State { public: - virtual void input(SDL_keysym key) {}; - virtual void tick() {}; - virtual void render(SDL_Surface* screen) {}; + virtual State* operator() (SDL_Renderer* renderer) {return NULL;}; }; -#endif +#endif \ No newline at end of file diff --git a/titlestate.cpp b/titlestate.cpp index 5a9b91e..8375c4a 100644 --- a/titlestate.cpp +++ b/titlestate.cpp @@ -1,52 +1,44 @@ -#include "includes.h" +#include "titlestate.h" +#include "util.h" -TitleState::TitleState() +State* TitleState::operator() (SDL_Renderer* renderer) { - background = SDL_LoadBMP("resources/title.bmp"); - pointer = SDL_LoadBMP("resources/pointer.bmp"); - - selection = 0; -} - -void TitleState::input(SDL_keysym key) -{ - if ((key.sym == SDLK_UP) && (selection != 0)) + SDL_Texture* background = loadImage(renderer, "resources/title.bmp"); + SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); + int selection = 0; + SDL_Event e; + + for (;;) { - selection--; - } else if ((key.sym == SDLK_DOWN) && (selection != 3)) - { - selection++; - } else if (key.sym == SDLK_RETURN) - { - switch (selection) + while (SDL_PollEvent(&e)) { - case 0: - changeState(new GameState()); - - break; - case 1: - changeState(new HowToPlayState()); - - break; - case 2: - changeState(new ChooseHighscoreListState()); - - break; - case 3: - exit(0); + if (e.type == SDL_QUIT) + { + return NULL; + } else if (e.type == SDL_KEYDOWN) + { + if ((e.key.keysym.sym == SDLK_UP) && (selection != 0)) + { + selection--; + } else if ((e.key.keysym.sym == SDLK_DOWN) && (selection != 3)) + { + selection++; + } else if (e.key.keysym.sym == SDLK_RETURN) + { + switch (selection) + { + //case 0: return new GameState(); + //case 1: return new HowToPlayState(); + //case 2: return new ChooseHighscoreListState(); + case 3: return NULL; + } + } + } } + + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, background, NULL, NULL); + applyTexture(renderer, pointer, 136, selection==0?316:(selection==1?350:(selection==2?381:417))); + SDL_RenderPresent(renderer); } -} - -void TitleState::render(SDL_Surface* screen) -{ - SDL_BlitSurface(background, NULL, screen, NULL); - - SDL_Rect pSpace; - pSpace.x = 136; - pSpace.y = (selection==0?316:(selection==1?350:(selection==2?381:417))); - pSpace.w = pointer->w; - pSpace.h = pointer->h; - - SDL_BlitSurface(pointer, NULL, screen, &pSpace); -} +} \ No newline at end of file diff --git a/titlestate.h b/titlestate.h index 6519ecf..11d16d7 100644 --- a/titlestate.h +++ b/titlestate.h @@ -1,16 +1,12 @@ +#include +#include "state.h" + #ifndef TITLESTATE_H #define TITLESTATE_H class TitleState : public State { public: - TitleState(); - void input(SDL_keysym key); - void render(SDL_Surface* screen); - - private: - SDL_Surface* background; - SDL_Surface* pointer; - int selection; + State* operator() (SDL_Renderer* renderer); }; -#endif +#endif \ No newline at end of file diff --git a/util.cpp b/util.cpp new file mode 100644 index 0000000..b021146 --- /dev/null +++ b/util.cpp @@ -0,0 +1,69 @@ +#include "util.h" +#include "mazeoflife.h" +#include + +void wrap(int* x, int* y) +{ + if (*x < 0) + { + *x = WIDTH-(0-*x); + } else if (*y < 0) + { + *y = HEIGHT-(0-*y); + } else if (*x >= WIDTH) + { + *x = *x-WIDTH; + } else if (*y >= HEIGHT) + { + *y = *y-HEIGHT; + } +} + +TTF_Font* loadFont(int size) +{ + TTF_Font* tmpfont = TTF_OpenFont("resources/mono.ttf", size); + + if (tmpfont == NULL) + { + printf("Unable to load font: %s\n", TTF_GetError()); + exit(1); + } + + return tmpfont; +} + +const char* getDataFile() +{ +#ifdef WINDOWS + char* dir = getenv("USERPROFILE"); +#else + char* dir = getenv("HOME"); +#endif + + return (std::string(dir) + "/.molhslist").c_str(); +} + +SDL_Texture* loadImage(SDL_Renderer* renderer, std::string file) +{ + SDL_Surface* surface = SDL_LoadBMP(file.c_str()); + if (surface == NULL) + { + std::cout << SDL_GetError() << std::endl; + return NULL; + } + + SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); + SDL_FreeSurface(surface); + + return texture; +} + +void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y) +{ + SDL_Rect pos; + pos.x = x; + pos.y = y; + SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h); + + SDL_RenderCopy(renderer, tex, NULL, &pos); +} \ No newline at end of file diff --git a/util.h b/util.h new file mode 100644 index 0000000..365a759 --- /dev/null +++ b/util.h @@ -0,0 +1,14 @@ +#include +#include +#include + +#ifndef UTIL_H +#define UTIL_H + +void wrap(int* x, int* y); +TTF_Font* loadFont(int size); +const char* getDataFile(); +SDL_Texture* loadImage(SDL_Renderer* renderer, std::string file); +void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y); + +#endif \ No newline at end of file diff --git a/winres.rc b/winres.rc deleted file mode 100644 index 1bc463a..0000000 --- a/winres.rc +++ /dev/null @@ -1 +0,0 @@ -IDI_ICON1 ICON DISCARDABLE "icon.ico" -- cgit 1.4.1