|
| 1 | +class engine{ |
| 2 | + private: |
| 3 | + // Screen width and height |
| 4 | + unsigned short int SCREEN_WIDTH, SCREEN_HEIGHT; |
| 5 | + SDL_Window *window; |
| 6 | + |
| 7 | + bool debug; |
| 8 | + |
| 9 | + bool gfx_init; |
| 10 | + bool sfx_init; |
| 11 | + |
| 12 | + // Used for calculation of FPS in current second, do not use this! |
| 13 | + uint16_t fps; |
| 14 | + |
| 15 | + // Used to store result for FPS in last second. Use this one! |
| 16 | + uint16_t fps_count; |
| 17 | + |
| 18 | + // Number of FPS we're limiting to, if at all |
| 19 | + uint8_t fps_limit; |
| 20 | + |
| 21 | + // How many milliseconds between frames |
| 22 | + uint8_t frame_interval; |
| 23 | + |
| 24 | + // When did we last draw a frame? |
| 25 | + uint32_t last_drawn_frame_timestamp; |
| 26 | + |
| 27 | + void calculate_fps(void); |
| 28 | + public: |
| 29 | + engine(int width, int height, uint8_t max_fps); |
| 30 | + bool init_gfx(void); |
| 31 | + bool init_sfx(void); |
| 32 | + bool clear_screen(void); |
| 33 | + void destroy(void); |
| 34 | + void flush_screen(void); |
| 35 | + void disable_debug_mode(void); |
| 36 | + SDL_Renderer *renderer; |
| 37 | + void loop(void); |
| 38 | + void draw_square(int x1, int y1, int x2, int y2, SDL_Color *color); |
| 39 | + void debug_message(const char* msg); |
| 40 | + unsigned short int get_height(void); |
| 41 | + unsigned short int get_width(void); |
| 42 | + |
| 43 | +}; |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +/** |
| 49 | +* Initializer for engine object |
| 50 | +* @param {integer} width - screen width to initialize with |
| 51 | +* @param {integer} height - screen height to initialize with |
| 52 | +*/ |
| 53 | +engine::engine( int width, int height, uint8_t max_fps ){ |
| 54 | + SCREEN_WIDTH = width; |
| 55 | + SCREEN_HEIGHT = height; |
| 56 | + window = NULL; |
| 57 | + renderer = NULL; |
| 58 | + gfx_init = false; |
| 59 | + fps=0; |
| 60 | + fps_count=0; |
| 61 | + last_drawn_frame_timestamp=0; |
| 62 | + fps_limit = max_fps; |
| 63 | + frame_interval = (max_fps == 0 ? 0 : (int)(1000 / fps_limit)); |
| 64 | + debug = true; |
| 65 | +} |
| 66 | + |
| 67 | +unsigned short int engine::get_height(void){ |
| 68 | + return SCREEN_HEIGHT; |
| 69 | +} |
| 70 | + |
| 71 | +unsigned short int engine::get_width(void){ |
| 72 | + return SCREEN_WIDTH; |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +void engine::debug_message(const char* msg){ |
| 77 | + if (debug){ |
| 78 | + printf(msg); |
| 79 | + fflush(stdout); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +bool engine::init_gfx(void){ |
| 86 | + printf("Init Video\n"); |
| 87 | + if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
| 88 | + printf( "SDL init failed.\n" ); |
| 89 | + exit(2); |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + printf("Creating Window\n"); |
| 94 | + window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_HEIGHT,SCREEN_WIDTH, SDL_WINDOW_FULLSCREEN); |
| 95 | + if (window == NULL){ |
| 96 | + printf("Window generating failed\n"); |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + printf("Creating renderer\n"); |
| 101 | + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); |
| 102 | + if (renderer == NULL){ |
| 103 | + printf("Failed to create renderer\n"); |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + printf("GFX init complete\n"); |
| 108 | + gfx_init = true; |
| 109 | + |
| 110 | + SDL_SetRenderDrawColor(renderer,0,0,0,SDL_ALPHA_OPAQUE); |
| 111 | + |
| 112 | + return true; |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +void engine::flush_screen(void){ |
| 117 | + if (gfx_init){ |
| 118 | + SDL_RenderPresent(renderer); |
| 119 | + } |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +bool engine::clear_screen(void){ |
| 124 | + if (gfx_init){ |
| 125 | + SDL_RenderClear(renderer); |
| 126 | + return true; |
| 127 | + } |
| 128 | + return false; |
| 129 | + |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +void engine::destroy(void){ |
| 134 | + if (gfx_init){ |
| 135 | + SDL_DestroyRenderer(renderer); |
| 136 | + SDL_DestroyWindow(window); |
| 137 | + } |
| 138 | + if (sfx_init){ |
| 139 | + Mix_Quit(); |
| 140 | + } |
| 141 | + SDL_Quit(); |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +void engine::disable_debug_mode(void){ |
| 146 | + SDL_LogSetAllPriority(SDL_LOG_PRIORITY_CRITICAL); |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | +/** |
| 151 | +* function basically calculates current framerate |
| 152 | +* detects when a second has passed and resets the counter |
| 153 | +* applies whatever value we had. |
| 154 | +*/ |
| 155 | +void engine::calculate_fps(void){ |
| 156 | + if( (int)SDL_GetTicks()/1000 > (int)last_drawn_frame_timestamp/1000 ){ |
| 157 | + fps_count = fps; |
| 158 | + fps = 0; |
| 159 | + } |
| 160 | + fps++; |
| 161 | +} |
| 162 | + |
| 163 | + |
| 164 | +void engine::loop(void){ |
| 165 | + // Calculate what tick are we drawing the next frame at |
| 166 | + uint32_t next_frame_timestamp = last_drawn_frame_timestamp+frame_interval; |
| 167 | + |
| 168 | + // Make sure the time we're supposed to draw at is upon us or passed: |
| 169 | + if (SDL_GetTicks() >= next_frame_timestamp){ |
| 170 | + |
| 171 | + // Calculate FPS |
| 172 | + calculate_fps(); |
| 173 | + |
| 174 | + // Make sure to get the new window surface and update it |
| 175 | + flush_screen(); |
| 176 | + |
| 177 | + // Update last drawn timestamp |
| 178 | + last_drawn_frame_timestamp = SDL_GetTicks(); |
| 179 | + |
| 180 | + } else { |
| 181 | + // So for this function I'm calculating WHEN we should draw the next frame |
| 182 | + // And using SDL_Delay to wait from now until then, to then draw the next frame. |
| 183 | + SDL_Delay( (next_frame_timestamp) - SDL_GetTicks() ); |
| 184 | + |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | + |
| 189 | +void engine::draw_square(int x1, int y1, int x2, int y2, SDL_Color *color){ |
| 190 | + SDL_Rect square; |
| 191 | + square.x = x1; |
| 192 | + square.y = y1; |
| 193 | + square.w = x2-x1; |
| 194 | + square.h = y2-y1; |
| 195 | + SDL_SetRenderDrawColor(renderer, color->r,color->g,color->b, color->a); |
| 196 | + SDL_RenderFillRect(renderer, &square); |
| 197 | +} |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | +bool engine::init_sfx(void){ |
| 202 | + if (SDL_Init(SDL_INIT_AUDIO) < 0){ |
| 203 | + printf("Unable to initialize audio\n"); |
| 204 | + return false; |
| 205 | + } |
| 206 | + |
| 207 | + if (Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,128) == -1){ |
| 208 | + printf("Unable to initialize SDL_Mixer\n"); |
| 209 | + return false; |
| 210 | + } |
| 211 | + |
| 212 | + printf("Init Audio\n"); |
| 213 | + sfx_init = true; |
| 214 | + return true; |
| 215 | +} |
| 216 | + |
0 commit comments