Add frontend/sdl
This commit is contained in:
parent
db0de92ff8
commit
eae04d668c
134
src/frontend/sdl/main.c
Normal file
134
src/frontend/sdl/main.c
Normal file
@ -0,0 +1,134 @@
|
||||
// This is the SDL2 software frontend.
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
// EVENTS
|
||||
struct ENGINE_EVENT * engine_event_get() {
|
||||
struct ENGINE_EVENT * event = engine_memory_alloc(NULL,sizeof(struct ENGINE_EVENT));
|
||||
SDL_Event sdlevent;
|
||||
|
||||
if (!SDL_PollEvent(&sdlevent)) {
|
||||
struct ENGINE_EVENT_NONE * data = engine_memory_alloc(NULL,sizeof(struct ENGINE_EVENT_NONE));
|
||||
event->type = ENGINE_EVENT_TYPE_NONE;
|
||||
event->data = data;
|
||||
return event;
|
||||
}
|
||||
|
||||
if (sdlevent.type == SDL_QUIT) {
|
||||
struct ENGINE_EVENT_EXIT * data = engine_memory_alloc(NULL,sizeof(struct ENGINE_EVENT_EXIT));;
|
||||
event->type = ENGINE_EVENT_TYPE_EXIT;
|
||||
event->data = data;
|
||||
return event;
|
||||
}
|
||||
|
||||
if (sdlevent.type == SDL_KEYDOWN || sdlevent.type == SDL_KEYUP) {
|
||||
if (sdlevent.key.repeat != 0) { goto unknown; }
|
||||
struct ENGINE_EVENT_INPUT_KB * data = engine_memory_alloc(NULL,sizeof(struct ENGINE_EVENT_INPUT_KB));
|
||||
data->pressed = (sdlevent.type == SDL_KEYDOWN);
|
||||
data->key = sdlevent.key.keysym.scancode;
|
||||
event->type = ENGINE_EVENT_TYPE_INPUT_KB;
|
||||
event->data = data;
|
||||
return event;
|
||||
}
|
||||
|
||||
unknown:
|
||||
struct ENGINE_EVENT_UNKNOWN * data = engine_memory_alloc(NULL,sizeof(struct ENGINE_EVENT_INPUT_KB));
|
||||
event->type = ENGINE_EVENT_TYPE_UNKNOWN;
|
||||
event->data = data;
|
||||
return event;
|
||||
}
|
||||
|
||||
// TEXTURE
|
||||
struct ENGINE_TEXTURE * engine_texture_create(int width,int height) {
|
||||
struct ENGINE_TEXTURE * texture = engine_memory_alloc(NULL,sizeof(struct ENGINE_TEXTURE));
|
||||
struct ENGINE_FRONTEND_TEXTURE * fe_texture = engine_memory_alloc(NULL,sizeof(struct ENGINE_FRONTEND_TEXTURE));
|
||||
fe_texture->surface = NULL;
|
||||
unsigned char * pixels = engine_memory_alloc(NULL,sizeof(unsigned char) * width * height * 4);
|
||||
memset(pixels,0,sizeof(unsigned char) * width * height * 4);
|
||||
fe_texture->pixels = pixels;
|
||||
texture->width = width;
|
||||
texture->height = height;
|
||||
texture->fe_texture = fe_texture;
|
||||
return texture;
|
||||
}
|
||||
|
||||
void engine_texture_destroy(struct ENGINE_TEXTURE * engine_texture) {
|
||||
engine_memory_free(engine_texture -> fe_texture -> pixels);
|
||||
engine_memory_free(engine_texture -> fe_texture);
|
||||
engine_memory_free(engine_texture);
|
||||
}
|
||||
|
||||
struct ENGINE_COLOR engine_texture_color_get(struct ENGINE_TEXTURE * engine_texture,int x,int y) {
|
||||
struct ENGINE_COLOR color;
|
||||
Uint32 pixelIndex = (x + (y * engine_texture -> width)) * 4;
|
||||
unsigned char * pixels = engine_texture -> fe_texture -> pixels;
|
||||
color.r = pixels[pixelIndex];
|
||||
color.g = pixels[pixelIndex + 1];
|
||||
color.b = pixels[pixelIndex + 2];
|
||||
color.a = pixels[pixelIndex + 3];
|
||||
return color;
|
||||
}
|
||||
|
||||
struct ENGINE_TEXTURE * engine_rendertarget;
|
||||
|
||||
// COLOR
|
||||
struct ENGINE_COLOR engine_frontend_color;
|
||||
Uint32 engine_frontend_fbcolor;
|
||||
void engine_color_set(unsigned char r,unsigned char g,unsigned char b,unsigned char a) {
|
||||
if (engine_rendertarget->fe_texture->surface != NULL) {
|
||||
engine_frontend_fbcolor = SDL_MapRGBA(engine_rendertarget->fe_texture->surface->format,r,g,b,a);
|
||||
}
|
||||
engine_frontend_color.r = r;
|
||||
engine_frontend_color.g = g;
|
||||
engine_frontend_color.b = b;
|
||||
engine_frontend_color.a = a;
|
||||
}
|
||||
|
||||
// RENDERTARGET
|
||||
void engine_rendertarget_set(struct ENGINE_TEXTURE * texture) {
|
||||
engine_rendertarget = texture;
|
||||
}
|
||||
|
||||
void engine_rendertarget_draw_point(int x,int y) {
|
||||
if (engine_rendertarget->fe_texture->surface != NULL) {
|
||||
Uint32 * pixels = engine_rendertarget->fe_texture->surface->pixels;
|
||||
pixels[x + y * engine_rendertarget -> width] = engine_frontend_fbcolor;
|
||||
} else {
|
||||
Uint32 pixelIndex = (x + (y * engine_rendertarget -> width)) * 4;
|
||||
unsigned char * pixels = engine_rendertarget -> fe_texture -> pixels;
|
||||
pixels[pixelIndex] = engine_frontend_color.r;
|
||||
pixels[pixelIndex + 1] = engine_frontend_color.g;
|
||||
pixels[pixelIndex + 2] = engine_frontend_color.b;
|
||||
pixels[pixelIndex + 3] = engine_frontend_color.a;
|
||||
}
|
||||
}
|
||||
|
||||
// TIME
|
||||
long long engine_time_get() {
|
||||
return SDL_GetTicks();
|
||||
}
|
||||
|
||||
void engine_time_sleep(int ms) {
|
||||
SDL_Delay(ms);
|
||||
}
|
||||
|
||||
// WINDOW
|
||||
struct ENGINE_WINDOW * engine_window_create(int width,int height,char * title) {
|
||||
struct ENGINE_WINDOW * window = engine_memory_alloc(NULL,sizeof(struct ENGINE_WINDOW));
|
||||
struct ENGINE_TEXTURE * texture = engine_texture_create(width,height);
|
||||
struct ENGINE_FRONTEND_WINDOW * fe_window = engine_memory_alloc(NULL,sizeof(struct ENGINE_FRONTEND_WINDOW));
|
||||
fe_window->window = SDL_CreateWindow(title,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,0);
|
||||
texture->fe_texture->surface = SDL_GetWindowSurface(fe_window->window);
|
||||
window->texture = texture;
|
||||
window->fe_window = fe_window;
|
||||
return window;
|
||||
}
|
||||
|
||||
void engine_window_present(struct ENGINE_WINDOW * window) {
|
||||
SDL_UpdateWindowSurface(window->fe_window->window);
|
||||
}
|
||||
|
||||
void engine_window_destroy(struct ENGINE_WINDOW * window) {
|
||||
engine_memory_free(window -> texture);
|
||||
engine_memory_free(window -> fe_window);
|
||||
engine_memory_free(window);
|
||||
}
|
2
src/frontend/sdl/structs.c
Normal file
2
src/frontend/sdl/structs.c
Normal file
@ -0,0 +1,2 @@
|
||||
struct ENGINE_FRONTEND_TEXTURE { unsigned char * pixels; struct SDL_Surface * surface; };
|
||||
struct ENGINE_FRONTEND_WINDOW { struct SDL_Window * window; };
|
@ -1,9 +1,9 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "frontend/null/structs.c"
|
||||
#include "frontend/sdl/structs.c"
|
||||
//#include "frontend/sdl/structs.c"
|
||||
#include "engine.c"
|
||||
#include "frontend/null/main.c"
|
||||
#include "frontend/sdl/main.c"
|
||||
#include "helpers.c"
|
||||
//#include "frontend/sdl/main.c"
|
||||
#include "lua.c"
|
||||
|
Loading…
Reference in New Issue
Block a user