| line# | code(run#) |
|---|---|
| 1 | #include <SDL.h> |
| 2 | #include <SDL_image.h> |
| 3 | #include <SDL_ttf.h> |
| 4 | #include <math.h> |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | #include "menu.h" |
| 8 | #include "file.h" |
| 9 | #include "play.h" |
| 10 | #include "edit.h" |
| 11 | #include "input.h" |
| 12 | |
| 13 | #include "debugmalloc.h" |
| 14 | |
| 15 | // Initialize SDL. Returns renderer pointer on success, returns NULL on faliure |
| 16 | SDL_Renderer *initSDL()(1) |
| 17 | { |
| 18 | if (SDL_Init(SDL_INIT_EVERYTHING) < 0)(1) |
| 19 | { |
| 20 | return NULL;(0) |
| 21 | } |
| 22 | // 20 x 12 blocks |
| 23 | SDL_Window *window = SDL_CreateWindow("Sokoban", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 768, 0);(1) |
| 24 | if (window == NULL)(1) |
| 25 | { |
| 26 | return NULL;(0) |
| 27 | } |
| 28 | SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);(1) |
| 29 | return renderer;(1) |
| 30 | } |
| 31 | |
| 32 | // Main program function |
| 33 | // Return values: 0 - success, 1 - SDL init error, 2 - SDL_Image error, 3 - TTF_Font error |
| 34 | int main(void)(1) |
| 35 | { |
| 36 | SDL_Renderer *renderer = initSDL();(1) |
| 37 | if (renderer == NULL)(1) |
| 38 | { |
| 39 | printf("ERROR: Couldn't initialize SDL\n");(0) |
| 40 | return 1;(0) |
| 41 | } |
| 42 | |
| 43 | SDL_Texture *tiles = IMG_LoadTexture(renderer, "tiles.png");(1) |
| 44 | if (tiles == NULL)(1) |
| 45 | { |
| 46 | printf("ERROR: Couldn't load tiles texture\n");(0) |
| 47 | return 2;(0) |
| 48 | } |
| 49 | |
| 50 | TTF_Init();(1) |
| 51 | TTF_Font *font = TTF_OpenFont("font.ttf", 50);(1) |
| 52 | if (!font)(1) |
| 53 | { |
| 54 | printf("ERROR: Couldn't open font\n");(0) |
| 55 | return 3;(0) |
| 56 | } |
| 57 | |
| 58 | int result; |
| 59 | char filename[64]; |
| 60 | filename[0] = '\0';(1) |
| 61 | |
| 62 | do |
| 63 | { |
| 64 | result = mainMenu(renderer, tiles, font, filename);(2) |
| 65 | switch (result)(2) |
| 66 | { |
| 67 | case 1:(1) |
| 68 | result = playLevel(renderer, tiles, font, filename);(1) |
| 69 | break;(1) |
| 70 | case 2:(0) |
| 71 | result = editLevel(renderer, tiles, font, filename);(0) |
| 72 | break;(0) |
| 73 | default:(1) |
| 74 | break;(1) |
| 75 | } |
| 76 | } while (result != 0);(2) |
| 77 | |
| 78 | SDL_DestroyTexture(tiles);(1) |
| 79 | TTF_CloseFont(font);(1) |
| 80 | SDL_Quit();(1) |
| 81 | |
| 82 | return 0;(1) |
| 83 | } |