Single thread + inputs
This commit is contained in:
+3
-2
@@ -23,16 +23,17 @@ set(SRC
|
||||
src/main.c
|
||||
src/renderer.c
|
||||
src/text.c
|
||||
src/input.c
|
||||
)
|
||||
|
||||
# add the executable
|
||||
add_executable(FLOPPE_FION ${SRC})
|
||||
|
||||
|
||||
target_link_libraries(FLOPPE_FION SDL2 SDL2_image SDL2_ttf SDL2_mixer pthread m)
|
||||
target_link_libraries(FLOPPE_FION SDL2 SDL2_image SDL2_ttf SDL2_mixer m)
|
||||
|
||||
|
||||
set_property(TARGET FLOPPE_FION PROPERTY C_STANDARD 23)
|
||||
|
||||
|
||||
file(COPY assets DESTINATION ${CMAKE_BINARY_DIR})
|
||||
file(COPY assets DESTINATION ${CMAKE_BINARY_DIR})
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
#include "input.h"
|
||||
#include <SDL2/SDL_scancode.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
keyEvent_t * events[SDL_NUM_SCANCODES];
|
||||
int event_counts[SDL_NUM_SCANCODES];
|
||||
|
||||
void input_init(void) {
|
||||
memset(events, 0, SDL_NUM_SCANCODES * sizeof(keyEvent_t *));
|
||||
memset(event_counts, 0, SDL_NUM_SCANCODES * sizeof(int));
|
||||
}
|
||||
|
||||
void input_poll(SDL_Scancode code) {
|
||||
printf("Scancode: %d\n", code);
|
||||
keyEvent_t * handlers = events[code];
|
||||
for(int i = 0; i < event_counts[code]; i++) {
|
||||
handlers[i]();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void input_listen(SDL_Scancode code, keyEvent_t handler) {
|
||||
event_counts[code]++;
|
||||
events[code] = (keyEvent_t *)realloc(events[code], event_counts[code] * sizeof(keyEvent_t));
|
||||
events[code][event_counts[code] - 1] = handler;
|
||||
}
|
||||
|
||||
void input_removeListener(SDL_Scancode code, keyEvent_t handler) {
|
||||
bool found = false;
|
||||
for(int i = 0; i < event_counts[code]; i++) {
|
||||
if(events[code][i] == handler) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
if(found) {
|
||||
if(i == event_counts[code] - 1) {
|
||||
break;
|
||||
}
|
||||
events[code][i] = events[code][i + 1];
|
||||
}
|
||||
}
|
||||
event_counts[code]--;
|
||||
}
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
#ifndef __INPUT_H__
|
||||
#define __INPUT_H__
|
||||
|
||||
#include <SDL2/SDL_scancode.h>
|
||||
|
||||
typedef void(*keyEvent_t)(void);
|
||||
|
||||
//renderer facing
|
||||
void input_init(void);
|
||||
void input_poll(SDL_Scancode);
|
||||
|
||||
//user facing
|
||||
void input_listen(SDL_Scancode code, keyEvent_t);
|
||||
void input_removeListener(SDL_Scancode code, keyEvent_t handler);
|
||||
|
||||
#endif
|
||||
|
||||
+68
-33
@@ -1,52 +1,87 @@
|
||||
#include "input.h"
|
||||
#include "renderer.h"
|
||||
#include "text.h"
|
||||
#include <SDL2/SDL_mixer.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_scancode.h>
|
||||
#include <SDL2/SDL_surface.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
long timecode;
|
||||
long iter;
|
||||
Mix_Music *prout;
|
||||
|
||||
static void space(void) {
|
||||
printf("space!\n");
|
||||
if(Mix_PlayMusic(prout, -1) != 0){
|
||||
printf("Music sound could not be played!\n"
|
||||
"SDL_Error: %s\n", Mix_GetError());
|
||||
Mix_FreeMusic(prout);
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void game_main(void) {
|
||||
long delta = time(NULL) - timecode;
|
||||
//main game code
|
||||
|
||||
if(iter == 0) {
|
||||
RenderObject_t text;
|
||||
SDL_Color white = {255, 255, 255, 255};
|
||||
TTF_Init();
|
||||
if(text_renderText(&text, 0.f, 0.f, 1000, 1, white, "./assets/fonts/arial.ttf", 25, "Hello World") < 0){
|
||||
printf("error");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
SDL_Surface* img = IMG_Load("./assets/img/pp.png");
|
||||
|
||||
RenderObject_t duck = {
|
||||
0.2,
|
||||
0.2,
|
||||
0,
|
||||
0.50,
|
||||
0.50,
|
||||
0,
|
||||
img
|
||||
};
|
||||
|
||||
renderer_renderObject(text);
|
||||
renderer_renderObject(duck);
|
||||
|
||||
input_listen(SDL_SCANCODE_SPACE, space);
|
||||
}
|
||||
|
||||
|
||||
//update time
|
||||
timecode = time(NULL);
|
||||
iter++;
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
renderer_main();
|
||||
Mix_Init(MIX_INIT_MP3);
|
||||
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) != 0){
|
||||
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) != 0){
|
||||
printf("Music sound could not be open!\n"
|
||||
"SDL_Error: %s\n", Mix_GetError());
|
||||
return 0;
|
||||
"SDL_Error: %s\n", Mix_GetError());
|
||||
running = false;
|
||||
}
|
||||
|
||||
Mix_Music *music = Mix_LoadMUS("./assets/music/pet.mp3");
|
||||
if(Mix_PlayMusic(music, -1) != 0){
|
||||
printf("Music sound could not be played!\n"
|
||||
"SDL_Error: %s\n", Mix_GetError());
|
||||
Mix_FreeMusic(music);
|
||||
return 0;
|
||||
}
|
||||
|
||||
RenderObject_t text;
|
||||
SDL_Color white = {255, 255, 255, 255};
|
||||
TTF_Init();
|
||||
if(text_renderText(&text, 0.f, 0.f, 1000, 1, white, "./assets/fonts/arial.ttf", 25, "Hello World") < 0){
|
||||
printf("error");
|
||||
prout = Mix_LoadMUS("./assets/music/pet.mp3");
|
||||
if(prout == NULL) {
|
||||
fprintf(stderr, "Prout not found!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
SDL_Surface* img = IMG_Load("./assets/img/pp.png");
|
||||
iter = 0;
|
||||
timecode = time(NULL);
|
||||
|
||||
RenderObject_t duck = {
|
||||
0.2,
|
||||
0.2,
|
||||
0,
|
||||
0.50,
|
||||
0.50,
|
||||
0,
|
||||
img
|
||||
};
|
||||
Mix_Init(MIX_INIT_MP3);
|
||||
renderer_main(game_main);
|
||||
|
||||
Mix_FreeMusic(prout);
|
||||
|
||||
renderer_renderObject(text);
|
||||
renderer_renderObject(duck);
|
||||
while(1);
|
||||
Mix_FreeMusic(music);
|
||||
Mix_CloseAudio();
|
||||
|
||||
text_freeFontCache();
|
||||
}
|
||||
|
||||
+38
-15
@@ -11,7 +11,7 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
#include <pthread.h>
|
||||
#include "input.h"
|
||||
|
||||
typedef struct RenderItem {
|
||||
RenderObject_t object;
|
||||
@@ -28,12 +28,17 @@ pthread_t thread;
|
||||
pthread_mutex_t mutex;
|
||||
SDL_Window* win;
|
||||
|
||||
|
||||
bool running;
|
||||
SDL_Renderer * renderer;
|
||||
|
||||
|
||||
static int compartSort(const RenderItem_t * a, const RenderItem_t * b) {
|
||||
return a->object.z - b->object.z;
|
||||
}
|
||||
|
||||
//int (*compar)(const void [.size], const void [.size], void *)
|
||||
static void * render_thread(void *) {
|
||||
void renderer_main(void(*game_main)(void)) {
|
||||
// returns zero on success else non-zero
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||
printf("error initializing SDL: %s\n", SDL_GetError());
|
||||
@@ -56,10 +61,38 @@ static void * render_thread(void *) {
|
||||
objectStorageSize = 1000;
|
||||
objects = malloc(objectStorageSize);
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
input_init();
|
||||
|
||||
running = true;
|
||||
|
||||
while(running) {
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
game_main();
|
||||
|
||||
SDL_Event event;
|
||||
|
||||
// Events management
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
|
||||
case SDL_QUIT:
|
||||
// handling of close button
|
||||
running = false;
|
||||
printf("Quitting my job!\n");
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
// keyboard API for key pressed
|
||||
input_poll(event.key.keysym.scancode);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//no need to make an extra render pass
|
||||
if(!running) break;
|
||||
|
||||
|
||||
while(true) {
|
||||
//SDL_RenderClear(renderer);
|
||||
for(int i = 0; i < objectCount; i++) {
|
||||
RenderItem_t * object = &objects[i];
|
||||
if(object->cache == NULL) {
|
||||
@@ -82,16 +115,6 @@ static void * render_thread(void *) {
|
||||
SDL_RenderPresent(renderer);
|
||||
SDL_Delay(1000 / 400); //400fps or u a pussy
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void renderer_main() {
|
||||
|
||||
pthread_mutex_init(&mutex, NULL);
|
||||
pthread_mutex_lock(&mutex);
|
||||
pthread_create(&thread, NULL, render_thread, NULL);
|
||||
pthread_mutex_lock(&mutex);
|
||||
pthread_mutex_destroy(&mutex);
|
||||
}
|
||||
|
||||
RenderId_t renderer_renderObject(RenderObject_t object) {
|
||||
|
||||
+5
-2
@@ -3,10 +3,13 @@
|
||||
|
||||
#include <SDL2/SDL_surface.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
typedef int RenderId_t;
|
||||
static SDL_Renderer * renderer;
|
||||
//goes to false on exit
|
||||
extern bool running;
|
||||
extern SDL_Renderer * renderer;
|
||||
|
||||
typedef struct RenderObject {
|
||||
float x,y;
|
||||
@@ -20,7 +23,7 @@ typedef struct MonitorSize {
|
||||
int width, height;
|
||||
} MonitorSize_t;
|
||||
|
||||
void renderer_main(void);
|
||||
void renderer_main(void(*game_main)(void)) ;
|
||||
RenderId_t renderer_renderObject(RenderObject_t);
|
||||
void renderer_removeObject(RenderId_t);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user