Files
dangerzone/include/engine.h
T

123 lines
2.0 KiB
C

#pragma once
#include <SDL3/SDL.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define WINDOW_NAME_MAX_LENGTH 128
#define TARGET_FRAME 1.0 / 60.0
#define FONT_ROWS 7
#define FONT_COLS 5
typedef struct {
char window_name[WINDOW_NAME_MAX_LENGTH];
uint16_t width, height;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
} engine_sdl_state_t;
typedef struct {
/**
* this function runs inside of the SDL_PollEvent loop
* useful for user input, quitting the program and so on
*/
void (*engine_poll_events)(void);
/**
* meant as the "area" for manupulating the framebuffer;
* after this is executed, the contents of
* game_state->framebuffer get drawn
*/
void (*engine_update_framebuffer)(void);
engine_sdl_state_t *sdl_state;
} engine_internal_state_t;
typedef struct {
SDL_Event event;
bool is_running;
bool framebuffer_dirty;
SDL_Rect dirty_rect;
uint16_t width, height;
uint32_t *framebuffer;
} game_state_t;
extern engine_internal_state_t *internal_state;
extern game_state_t *game_state;
engine_sdl_state_t*
engine_sdl_init(
uint32_t window_width,
uint32_t window_height,
const char window_name[]
);
void
engine_sdl_destroy(
engine_sdl_state_t *state
);
bool
engine_init(
uint32_t window_width,
uint32_t window_height,
const char window_name[],
void (*engine_poll_events)(void),
void (*engine_update_framebuffer)(void)
);
void
engine_start();
void
engine_stop();
void
put_pixel(
unsigned int x,
unsigned int y,
uint32_t color
);
void
put_rect(
uint32_t x,
uint32_t y,
uint32_t w,
uint32_t h,
uint32_t color
);
void
clear_framebuffer(
uint32_t color
);
void
clear_rect(int x, int y, int w, int h, uint32_t color);
void
mark_dirty_rect(int x, int y, int w, int h);
void
put_char(
char c,
uint32_t x,
uint32_t y,
uint32_t size,
uint32_t fg_color,
uint32_t bg_color
);
void
put_string(
const char *text,
uint32_t x,
uint32_t y,
uint32_t size,
uint32_t spacing,
uint32_t line_spacing,
uint32_t fg_color,
uint32_t bg_color
);