mirror of
https://github.com/thepigeongenerator/sdl_template.git
synced 2025-12-17 05:55:47 +01:00
rename most types to lowercase, shuffle things around about definition-wise and added more default definitions
This commit is contained in:
14
src/errors.c
14
src/errors.c
@@ -5,14 +5,12 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAX_STR_LEN 128
|
||||
|
||||
void error(const ErrorCode error_code, const char* format, ...) {
|
||||
char buffer[MAX_STR_LEN] = {0}; // contains the buffer of the final string
|
||||
void error(error_code const error_code, char const* const format, ...) {
|
||||
char buffer[ERROR_MAX_STR_LEN] = {0}; // contains the buffer of the final string
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buffer, MAX_STR_LEN, format, args);
|
||||
vsnprintf(buffer, ERROR_MAX_STR_LEN, format, args);
|
||||
va_end(args);
|
||||
|
||||
printf("\033[91mE\033[0m: %s\n", buffer);
|
||||
@@ -21,12 +19,12 @@ void error(const ErrorCode error_code, const char* format, ...) {
|
||||
exit(error_code);
|
||||
}
|
||||
|
||||
void warn(const char* format, ...) {
|
||||
char buffer[MAX_STR_LEN] = {0}; // contains the buffer of the final string
|
||||
void warn(char const* const format, ...) {
|
||||
char buffer[ERROR_MAX_STR_LEN] = {0}; // contains the buffer of the final string
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buffer, MAX_STR_LEN, format, args);
|
||||
vsnprintf(buffer, ERROR_MAX_STR_LEN, format, args);
|
||||
va_end(args);
|
||||
|
||||
printf("\033[93mW\033[0m: %s\n", buffer);
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
typedef unsigned char ErrorCode;
|
||||
#define ERROR_MAX_STR_LEN 128
|
||||
|
||||
typedef uint8_t error_code;
|
||||
enum {
|
||||
ERROR_MISC = -1,
|
||||
|
||||
@@ -21,5 +24,5 @@ enum {
|
||||
};
|
||||
|
||||
// call when a fatal error has occurred, the program will immediately terminate when called
|
||||
void error(const ErrorCode error_code, const char* format, ...);
|
||||
void warn(const char* format, ...);
|
||||
void error(error_code const error_code, char const* format, ...);
|
||||
void warn(char const* format, ...);
|
||||
|
||||
@@ -6,10 +6,12 @@
|
||||
#include "../main.h"
|
||||
|
||||
|
||||
// called every time the game's state is updated
|
||||
void game_update(GameData game_data, const uint8_t* keys) {
|
||||
(void)game_data;
|
||||
if (keys[SDL_SCANCODE_ESCAPE]) {
|
||||
stop();
|
||||
}
|
||||
void game_init(game_data* const dat) {
|
||||
*dat = (game_data){0};
|
||||
}
|
||||
|
||||
// called every time the game's state is updated
|
||||
void game_update(game_data* const dat, uint8_t const* const keys) {
|
||||
(void)dat;
|
||||
if (keys[SDL_SCANCODE_ESCAPE]) stop();
|
||||
}
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
// stores the data used in the game
|
||||
typedef struct {
|
||||
void* val;
|
||||
} GameData;
|
||||
} game_data;
|
||||
|
||||
// updates the game's state
|
||||
void game_update(GameData game_data, const uint8_t* keys);
|
||||
void game_init(game_data* game_dat);
|
||||
void game_update(game_data* game_dat, const uint8_t* keys);
|
||||
|
||||
29
src/main.c
29
src/main.c
@@ -4,8 +4,6 @@
|
||||
#include <SDL_error.h>
|
||||
#include <SDL_events.h>
|
||||
#include <SDL_keyboard.h>
|
||||
#include <SDL_render.h>
|
||||
#include <SDL_video.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
@@ -14,18 +12,12 @@
|
||||
|
||||
#include "errors.h"
|
||||
#include "game/game.h"
|
||||
// #include "window/audio.h"
|
||||
#include "window/renderer.h"
|
||||
|
||||
#ifdef __EMSCRIPTEN__ // for web builds
|
||||
#include <emscripten.h>
|
||||
#endif
|
||||
|
||||
|
||||
bool playing = true;
|
||||
|
||||
SDL_Window* window = NULL;
|
||||
SDL_Renderer* renderer = NULL;
|
||||
render_data render_dat = {0};
|
||||
game_data game_dat = {0};
|
||||
|
||||
// handles game application initialisation
|
||||
static void init(void) {
|
||||
@@ -35,15 +27,13 @@ static void init(void) {
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize the renderer
|
||||
if (renderer_init(&window, &renderer) < 0) {
|
||||
error(ERROR_SDL_RENDERER_INIT, SDL_GetError());
|
||||
return;
|
||||
}
|
||||
renderer_init(&render_dat, &game_dat);
|
||||
|
||||
// initialize audio
|
||||
// AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 255);
|
||||
|
||||
// AudioData audio1 = audio_load_wav(audio_device, "FILE NAME");
|
||||
game_init(&game_dat);
|
||||
}
|
||||
|
||||
// handles game application updating
|
||||
@@ -60,12 +50,9 @@ static void update(void) {
|
||||
}
|
||||
}
|
||||
|
||||
// updates the game
|
||||
game_update((GameData){NULL}, SDL_GetKeyboardState(NULL));
|
||||
|
||||
// updates the render
|
||||
RenderData render_data = {window, renderer};
|
||||
renderer_update(&render_data);
|
||||
// perform updates
|
||||
game_update(&game_dat, SDL_GetKeyboardState(NULL));
|
||||
renderer_update(&render_dat);
|
||||
}
|
||||
|
||||
// handles game application quitting
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// initializes the window and renderer
|
||||
#include "renderer.h"
|
||||
|
||||
#include <SDL_error.h>
|
||||
@@ -7,27 +6,29 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../errors.h"
|
||||
#include "../game/game.h"
|
||||
|
||||
|
||||
int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
|
||||
// initializes the window and renderer
|
||||
void renderer_init(render_data* const render_dat, game_data const* const game_dat) {
|
||||
// create a new window
|
||||
*window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 100, 100, SDL_WINDOW_SHOWN);
|
||||
if (*window == NULL) {
|
||||
SDL_Window* const window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 100, 100, SDL_WINDOW_SHOWN);
|
||||
if (window == NULL)
|
||||
error(ERROR_SDL_RENDERER_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
// create a renderer
|
||||
*renderer = SDL_CreateRenderer(*window, -1, SDL_RENDERER_PRESENTVSYNC);
|
||||
if (*renderer == NULL) {
|
||||
SDL_Renderer* const renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
|
||||
if (renderer == NULL)
|
||||
error(ERROR_SDL_RENDERER_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
*render_dat = (render_data){
|
||||
window,
|
||||
renderer,
|
||||
game_dat,
|
||||
};
|
||||
}
|
||||
|
||||
void renderer_update(const RenderData* render_data) {
|
||||
void renderer_update(const render_data* render_data) {
|
||||
SDL_Renderer* renderer = render_data->renderer;
|
||||
|
||||
int success = 0; // if an error occurs, this value is <0
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
#include <SDL_render.h>
|
||||
#include <SDL_video.h>
|
||||
|
||||
#include "../game/game.h"
|
||||
|
||||
typedef struct {
|
||||
SDL_Window* window;
|
||||
SDL_Renderer* renderer;
|
||||
} RenderData;
|
||||
game_data const* game_dat;
|
||||
} render_data;
|
||||
|
||||
int renderer_init(SDL_Window** window, SDL_Renderer** renderer);
|
||||
void renderer_update(const RenderData* render_data);
|
||||
void renderer_init(render_data* render_dat, game_data const* game_dat);
|
||||
void renderer_update(render_data const* render_data);
|
||||
|
||||
Reference in New Issue
Block a user