xhads/source/sdl2.c

182 lines
4.4 KiB
C
Raw Normal View History

2024-02-15 21:03:09 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
static SDL_Texture * * render_texture;
static int render_texture_count = 0;
static SDL_Window * window;
static SDL_Renderer * renderer;
static SDL_Texture * font;
static SDL_Texture * mono;
static void render_clean_up (void) {
int i;
for (i = 0; i < render_texture_count; ++i) {
SDL_DestroyTexture (render_texture [i]);
}
free (render_texture);
SDL_DestroyTexture (font);
SDL_DestroyTexture (mono);
SDL_DestroyRenderer (renderer);
SDL_DestroyWindow (window);
SDL_Quit ();
}
extern int signal_x;
extern int signal_y;
extern int cursor_mode;
extern int signal_mode;
extern int engine_active;
extern int window_width (void);
extern int window_height (void);
extern void render_sprite (int sprite, int x, int y, int u, int v, int width, int height);
extern void render_string (char * string, int x, int y, int colour, char monospace);
extern void engine_configure (void);
extern void engine_synchronize (void);
extern int import_sprite (char * path);
extern int sprite_width (int index);
extern int sprite_height (int index);
int signal_x = 0;
int signal_y = 0;
int cursor_mode = 0;
int signal_mode = 0;
int engine_active = 0;
int window_width (void) { int width = 0; SDL_GetWindowSize (window, & width, NULL); return (width); }
int window_height (void) { int height = 0; SDL_GetWindowSize (window, NULL, & height); return (height); }
void render_sprite (int sprite, int x, int y, int u, int v, int width, int height) {
SDL_Rect source, destination;
source.x = u;
source.y = v;
source.w = width;
source.h = height;
destination.x = x;
destination.y = y;
destination.w = (width < 0) ? -width : width;
destination.h = (height < 0) ? -height : height;
SDL_RenderCopy (renderer, render_texture [sprite], & source, & destination);
}
void render_string (char * string, int x, int y, int colour, char monospace) {
int i;
(void) colour;
for (i = 0; string [i] != '\0'; ++i) {
SDL_Rect source, destination;
source.x = (string [i] - 32) / 8 * 9;
source.y = (string [i] - 32) % 8 * 18;
source.w = 9;
source.h = 18;
destination.x = x + 7 + 9 * i;
destination.y = y + 7;
destination.w = 9;
destination.h = 18;
SDL_RenderCopy (renderer, (monospace != 0) ? mono : font, & source, & destination);
}
}
void engine_configure (void) {
engine_active = 1;
SDL_Init (SDL_INIT_VIDEO);
window = SDL_CreateWindow ("Chads of Might & Magic", 0, 0, 1800, 900, 0);
renderer = SDL_CreateRenderer (window, -1, SDL_RENDERER_ACCELERATED);
font = IMG_LoadTexture (renderer, "./sprite/font/font.png");
mono = IMG_LoadTexture (renderer, "./sprite/font/mono.png");
2024-02-15 21:03:09 -05:00
atexit (render_clean_up);
}
void engine_synchronize (void) {
SDL_Event event;
SDL_RenderPresent (renderer);
SDL_GetMouseState (& signal_x, & signal_y);
SDL_PollEvent (& event);
switch (event.type) {
case SDL_QUIT:
engine_active = 0;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) cursor_mode = 1;
if (event.button.button == SDL_BUTTON_RIGHT) cursor_mode = 2;
if (event.button.button == SDL_BUTTON_MIDDLE) cursor_mode = 3;
break;
case SDL_MOUSEBUTTONUP:
cursor_mode = 0;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_UP: signal_mode = 1; break;
case SDLK_DOWN: signal_mode = 2; break;
case SDLK_LEFT: signal_mode = 3; break;
case SDLK_RIGHT: signal_mode = 4; break;
case SDLK_ESCAPE: engine_active = 0; break;
default: signal_mode = 0; break;
}
break;
case SDL_KEYUP:
cursor_mode = 0;
break;
default:
break;
}
signal_mode = 0;
SDL_RenderClear (renderer);
}
int import_sprite (char * path) {
++render_texture_count;
render_texture = realloc (render_texture, (unsigned long int) render_texture_count * sizeof (* render_texture));
render_texture [render_texture_count - 1] = IMG_LoadTexture (renderer, path);
if (render_texture [render_texture_count - 1] == NULL) {
printf ("\033[1;31m%3i : '%60s';\033[0m\n", render_texture_count - 1, path);
}
return (render_texture_count - 1);
}
int sprite_width (int index) {
int width = 0;
SDL_QueryTexture (render_texture [index], NULL, NULL, & width, NULL);
return (width);
}
int sprite_height (int index) {
int height = 0;
SDL_QueryTexture (render_texture [index], NULL, NULL, NULL, & height);
return (height);
}