Created the Window and Renderer and started implementing the functions

for the game loop
This commit is contained in:
abdelrahman committed 2022-09-10 22:17:58 +01:00
1 parent a0d8b75c11
commit 33066b381e
11 files changed
+280 -24

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
#include "Game.h"
#include "SDL.h"
#include "SDL_events.h"
#include "SDL_pixels.h"
#include "Window.h"
#include "constants.h"
#include "engine_build_config.h"
#include <stdbool.h>
#ifdef DEBUG_ENGINE
#include <stdio.h>
#endif /* DEBUG_ENGINE */
INTERNAL struct game {
bool running;
Window window;
SDL_Event event;
} Game;
bool init_game(const char *game_name, int window_width, int window_height,
bool full_screen) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
#ifdef DEBUG_ENGINE
printf("Failed to initialise game\n");
#endif /* DEBUG_ENGINE */
return false;
}
if (!init_window(&(Game.window), game_name, window_width, window_height,
full_screen)) {
#ifdef DEBUG_ENGINE
printf("Failed to initialise game\n");
#endif /* DEBUG_ENGINE */
return false;
}
Game.running = true;
#ifdef DEBUG_ENGINE
printf("Game initialised successfully\n");
#endif /* DEBUG_ENGINE */
return true;
}
bool game_running() { return Game.running; }
void handle_events() {
while (SDL_PollEvent(&(Game.event))) {
switch (Game.event.type) {
case SDL_QUIT:
Game.running = false;
break;
}
}
}
void update() {}
void render() {
fill_window_with_color(&(Game.window), (SDL_Color){255, 255, 255, 255});
swap_render_buffers(&(Game.window));
}
void close_game() {
destroy_window(&(Game.window));
Game.running = false;
SDL_Quit();
#ifdef DEBUG_ENGINE
printf("Game closed successfully\n");
#endif /* DEBUG_ENGINE */
}
+58
View File
@@ -0,0 +1,58 @@
#include "Renderer.h"
#include "SDL.h"
#include "SDL_render.h"
#include "engine_build_config.h"
#include <stdbool.h>
#include <stdlib.h>
#ifdef DEBUG_ENGINE
#include <stdio.h>
#endif /* DEBUG_ENGINE */
typedef struct renderer {
SDL_Renderer *SDL_base_renderer;
} Renderer;
Renderer *create_renderer() { return (Renderer *)malloc(sizeof(Renderer)); }
bool init_renderer(Renderer *renderer, SDL_Window *window) {
if (!renderer || !window) {
return false;
}
renderer->SDL_base_renderer = SDL_CreateRenderer(
window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
// Technically speaking, I believe it should be possible to just return
// the pointer. However, to make sure that everything is clear, I am
// performing a test that returns true or false based on whether the
// pointer was initialised or not.
return renderer->SDL_base_renderer ? true : false;
}
void render_clear(const Renderer *renderer, SDL_Color color) {
SDL_SetRenderDrawColor(renderer->SDL_base_renderer, color.r, color.r, color.b,
color.a);
SDL_RenderClear(renderer->SDL_base_renderer);
}
void swap_buffers(const Renderer *renderer) {
SDL_RenderPresent(renderer->SDL_base_renderer);
}
void destroy_renderer(Renderer *renderer) {
if (renderer) {
if (renderer->SDL_base_renderer) {
SDL_DestroyRenderer(renderer->SDL_base_renderer);
renderer->SDL_base_renderer = NULL;
}
free(renderer);
renderer = NULL;
#ifdef DEBUG_ENGINE
printf("Renderer destroyed successfully\n");
#endif /* DEBUG_ENGINE */
}
}
+67
View File
@@ -0,0 +1,67 @@
#include "Window.h"
#include "Renderer.h"
#include "engine_build_config.h"
#include <stdbool.h>
#ifdef DEBUG_ENGINE
#include <stdio.h>
#endif /* DEBUG_ENGINE */
bool init_window(Window *wnd, const char *title, int width, int height,
bool full_screen) {
Uint32 window_flags = SDL_WINDOW_ALLOW_HIGHDPI;
if (full_screen) {
window_flags |= SDL_WINDOW_FULLSCREEN;
}
wnd->SDL_base_window =
SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height, window_flags);
if (!wnd->SDL_base_window) {
return false;
}
SDL_GetWindowSize(wnd->SDL_base_window, &(wnd->width), &(wnd->height));
wnd->renderer = create_renderer();
if (!wnd->renderer || !init_renderer(wnd->renderer, wnd->SDL_base_window)) {
destroy_window(wnd);
return false;
}
#ifdef DEBUG_ENGINE
printf("Renderer initialised successfully\n");
printf("Window initialised successfully with width %d and height %d\n",
wnd->width, wnd->height);
#endif /* DEBUG_ENGINE */
return true;
}
void fill_window_with_color(const Window *wnd, SDL_Color color) {
render_clear(wnd->renderer, color);
}
void swap_render_buffers(const Window *wnd) { swap_buffers(wnd->renderer); }
void destroy_window(Window *wnd) {
if (wnd->SDL_base_window) {
if (wnd->renderer) {
destroy_renderer(wnd->renderer);
}
SDL_DestroyWindow(wnd->SDL_base_window);
wnd->SDL_base_window = NULL;
wnd->width = 0;
wnd->height = 0;
#ifdef DEBUG_ENGINE
printf("Window destroyed successfully\n");
#endif /* DEBUG_ENGINE */
}
}
-13
View File
@@ -1,13 +0,0 @@
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include "SDL_ttf.h"
#include <stdio.h>
void init(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
printf("SDL initialiased successfully\n");
SDL_Quit();
}
}