diff options
Diffstat (limited to 'mazeoflife.cpp')
| -rw-r--r-- | mazeoflife.cpp | 141 |
1 files changed, 29 insertions, 112 deletions
| diff --git a/mazeoflife.cpp b/mazeoflife.cpp index 107a640..a020f6c 100644 --- a/mazeoflife.cpp +++ b/mazeoflife.cpp | |||
| @@ -1,7 +1,12 @@ | |||
| 1 | #include "includes.h" | 1 | #include "mazeoflife.h" |
| 2 | 2 | #include <SDL.h> | |
| 3 | SDL_Surface *screen; | 3 | #include <SDL_ttf.h> |
| 4 | State* state; | 4 | #include <SDL_net.h> |
| 5 | #include <cstdlib> | ||
| 6 | #include <ctime> | ||
| 7 | #include <iostream> | ||
| 8 | #include "state.h" | ||
| 9 | #include "titlestate.h" | ||
| 5 | 10 | ||
| 6 | int main(int argc, char *argv[]) | 11 | int main(int argc, char *argv[]) |
| 7 | { | 12 | { |
| @@ -24,118 +29,30 @@ int main(int argc, char *argv[]) | |||
| 24 | printf("Cound not initalize SDL_net: %s.\n", SDLNet_GetError()); | 29 | printf("Cound not initalize SDL_net: %s.\n", SDLNet_GetError()); |
| 25 | exit(-1); | 30 | exit(-1); |
| 26 | } | 31 | } |
| 27 | 32 | ||
| 28 | /* Clean up on exit */ | 33 | SDL_Window* window = SDL_CreateWindow("Maze of Life", 100, 100, 480, 480, SDL_WINDOW_SHOWN); |
| 29 | atexit(SDL_Quit); | 34 | if (window == NULL) |
| 30 | atexit(TTF_Quit); | ||
| 31 | atexit(SDLNet_Quit); | ||
| 32 | |||
| 33 | SDL_WM_SetCaption("Maze Of Life", NULL); | ||
| 34 | |||
| 35 | SDL_Surface* icon; | ||
| 36 | icon = SDL_LoadBMP("resources/icon.bmp"); | ||
| 37 | SDL_WM_SetIcon(icon, NULL); | ||
| 38 | |||
| 39 | /* | ||
| 40 | * Initialize the display in a 640x480 8-bit palettized mode, | ||
| 41 | * requesting a software surface | ||
| 42 | */ | ||
| 43 | screen = SDL_SetVideoMode(WIDTH*16, HEIGHT*16, 8, SDL_DOUBLEBUF); | ||
| 44 | if ( screen == NULL ) { | ||
| 45 | fprintf(stderr, "Couldn't set %dx%dx8 video mode: %s\n", WIDTH*16, WIDTH*16, SDL_GetError()); | ||
| 46 | exit(1); | ||
| 47 | } | ||
| 48 | |||
| 49 | SDL_EnableKeyRepeat(100, 70); | ||
| 50 | |||
| 51 | state = new TitleState(); | ||
| 52 | |||
| 53 | SDL_AddTimer(TICKDELAY, *tick, NULL); | ||
| 54 | |||
| 55 | SDL_Event anEvent; | ||
| 56 | for (;;) | ||
| 57 | { | 35 | { |
| 58 | while (SDL_PollEvent(&anEvent)) | 36 | std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; |
| 59 | { | 37 | return 1; |
| 60 | switch (anEvent.type) | ||
| 61 | { | ||
| 62 | case SDL_QUIT: | ||
| 63 | exit(0); | ||
| 64 | |||
| 65 | break; | ||
| 66 | case SDL_KEYDOWN: | ||
| 67 | if (anEvent.key.keysym.sym == SDLK_F4) | ||
| 68 | { | ||
| 69 | SDL_WM_ToggleFullScreen(screen); | ||
| 70 | } else { | ||
| 71 | state->input(anEvent.key.keysym); | ||
| 72 | } | ||
| 73 | |||
| 74 | break; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | 38 | } |
| 78 | 39 | ||
| 79 | exit(0); | 40 | SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); |
| 80 | } | 41 | if (renderer == NULL) |
| 81 | |||
| 82 | void wrap(int* x, int* y) | ||
| 83 | { | ||
| 84 | if (*x < 0) | ||
| 85 | { | ||
| 86 | *x = WIDTH-(0-*x); | ||
| 87 | } else if (*y < 0) | ||
| 88 | { | 42 | { |
| 89 | *y = HEIGHT-(0-*y); | 43 | std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; |
| 90 | } else if (*x >= WIDTH) | 44 | return 1; |
| 91 | { | ||
| 92 | *x = *x-WIDTH; | ||
| 93 | } else if (*y >= HEIGHT) | ||
| 94 | { | ||
| 95 | *y = *y-HEIGHT; | ||
| 96 | } | 45 | } |
| 97 | } | ||
| 98 | |||
| 99 | Uint32 getColor(int r, int g, int b) | ||
| 100 | { | ||
| 101 | return SDL_MapRGB(screen->format, r, g, b); | ||
| 102 | } | ||
| 103 | |||
| 104 | void changeState(State* nState) | ||
| 105 | { | ||
| 106 | state = nState; | ||
| 107 | } | ||
| 108 | |||
| 109 | Uint32 tick(Uint32 interval, void *param) | ||
| 110 | { | ||
| 111 | state->tick(); | ||
| 112 | state->render(screen); | ||
| 113 | 46 | ||
| 114 | SDL_Flip(screen); | 47 | State* state = new TitleState(); |
| 115 | 48 | while (state != NULL) | |
| 116 | return interval; | ||
| 117 | } | ||
| 118 | |||
| 119 | TTF_Font* loadFont(int size) | ||
| 120 | { | ||
| 121 | TTF_Font* tmpfont = TTF_OpenFont("resources/mono.ttf", size); | ||
| 122 | |||
| 123 | if (tmpfont == NULL) | ||
| 124 | { | 49 | { |
| 125 | printf("Unable to load font: %s\n", TTF_GetError()); | 50 | state = (*state)(renderer); |
| 126 | exit(1); | ||
| 127 | } | 51 | } |
| 128 | 52 | ||
| 129 | return tmpfont; | 53 | SDL_DestroyRenderer(renderer); |
| 130 | } | 54 | SDL_DestroyWindow(window); |
| 131 | 55 | SDLNet_Quit(); | |
| 132 | const char* getDataFile() | 56 | TTF_Quit(); |
| 133 | { | 57 | SDL_Quit(); |
| 134 | #ifdef WINDOWS | 58 | } \ No newline at end of file |
| 135 | char* dir = getenv("USERPROFILE"); | ||
| 136 | #else | ||
| 137 | char* dir = getenv("HOME"); | ||
| 138 | #endif | ||
| 139 | |||
| 140 | return (std::string(dir) + "/.molhslist").c_str(); | ||
| 141 | } | ||
