winter is comming: brace yourself
This commit is contained in:
+13
-84
@@ -5,40 +5,23 @@
|
||||
#include <SDL2/SDL_rect.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_timer.h>
|
||||
#include <bits/pthreadtypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
#include "input.h"
|
||||
|
||||
typedef struct RenderItem {
|
||||
RenderObject_t object;
|
||||
SDL_Texture * cache;
|
||||
SDL_Rect rect;
|
||||
RenderId_t id;
|
||||
} RenderItem_t;
|
||||
|
||||
RenderItem_t * objects = NULL;
|
||||
int objectCount = 0;
|
||||
int objectStorageSize = 0;
|
||||
MonitorSize_t monitorSize;
|
||||
pthread_t thread;
|
||||
pthread_mutex_t mutex;
|
||||
SDL_Window* win;
|
||||
|
||||
#include "scene.h"
|
||||
|
||||
bool running;
|
||||
MonitorSize_t monitorSize;
|
||||
SDL_Window* win;
|
||||
SDL_Renderer * renderer;
|
||||
|
||||
|
||||
static int compartSort(const RenderItem_t * a, const RenderItem_t * b) {
|
||||
return a->object.z - b->object.z;
|
||||
}
|
||||
Scene_t * scenes;
|
||||
int scenesCount = 0;
|
||||
|
||||
//int (*compar)(const void [.size], const void [.size], void *)
|
||||
void renderer_main(void(*game_main)(void)) {
|
||||
void renderer_main(void(*game_setup)(void), void(*game_loop)(void)) {
|
||||
// returns zero on success else non-zero
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||
printf("error initializing SDL: %s\n", SDL_GetError());
|
||||
@@ -58,17 +41,18 @@ void renderer_main(void(*game_main)(void)) {
|
||||
}
|
||||
|
||||
SDL_GetRendererOutputSize(renderer, &monitorSize.width, &monitorSize.height);
|
||||
objectStorageSize = 1000;
|
||||
objects = malloc(objectStorageSize);
|
||||
scenesCount = 10;
|
||||
scenes = malloc(scenesCount);
|
||||
|
||||
input_init();
|
||||
|
||||
running = true;
|
||||
|
||||
game_setup();
|
||||
while(running) {
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
game_main();
|
||||
game_loop();
|
||||
|
||||
SDL_Event event;
|
||||
|
||||
@@ -92,70 +76,15 @@ void renderer_main(void(*game_main)(void)) {
|
||||
//no need to make an extra render pass
|
||||
if(!running) break;
|
||||
|
||||
|
||||
for(int i = 0; i < objectCount; i++) {
|
||||
RenderItem_t * object = &objects[i];
|
||||
int error = SDL_RenderCopyEx(renderer, object->cache, NULL, &object->rect, object->object.angle_degre, NULL, SDL_FLIP_NONE);
|
||||
if(error != 0) {
|
||||
fprintf(stderr, "Render error in object %d with error %s\n", object->id, SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
for(int i = 0; i < scenesCount; i++) {
|
||||
scene_render(scenes + i);
|
||||
}
|
||||
SDL_RenderPresent(renderer);
|
||||
SDL_Delay(1000 / 400); //400fps or u a pussy
|
||||
}
|
||||
free(scenes);
|
||||
}
|
||||
|
||||
static void buildCache(RenderItem_t * object) {
|
||||
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, object->object.surface);
|
||||
if(texture == NULL) {
|
||||
fprintf(stderr, "Error while creating texture %s is_null: %d\n", SDL_GetError(), object->object.surface == NULL);
|
||||
exit(1);
|
||||
}
|
||||
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
|
||||
object->cache = texture;
|
||||
}
|
||||
|
||||
RenderId_t renderer_renderObject(RenderObject_t object) {
|
||||
RenderId_t itemId = objectCount++;
|
||||
|
||||
SDL_Rect rect = {
|
||||
round(object.x * monitorSize.width),
|
||||
round(object.y * monitorSize.height),
|
||||
round(object.width * monitorSize.width),
|
||||
round(object.height * monitorSize.height)
|
||||
};
|
||||
|
||||
RenderItem_t item = {
|
||||
object,
|
||||
NULL,
|
||||
rect,
|
||||
itemId
|
||||
};
|
||||
|
||||
buildCache(&item);
|
||||
|
||||
objects[itemId] = item;
|
||||
//the objects are sorted
|
||||
qsort(objects, objectCount, sizeof(RenderItem_t), (__compar_fn_t)compartSort);
|
||||
return itemId;
|
||||
}
|
||||
|
||||
void renderer_removeObject(RenderId_t id) {
|
||||
bool found = false;
|
||||
for(int i = 0; i < objectCount - 1; i++) {
|
||||
if(objects[i].id == id) {
|
||||
SDL_FreeSurface(objects[i].object.surface);
|
||||
if(objects[i].cache != NULL) {
|
||||
SDL_DestroyTexture(objects[i].cache);
|
||||
}
|
||||
}
|
||||
if(found) {
|
||||
objects[i] = objects[i + 1];
|
||||
}
|
||||
}
|
||||
objectCount--;
|
||||
}
|
||||
|
||||
MonitorSize_t renderer_getMonitorSize() {
|
||||
return monitorSize;
|
||||
|
||||
Reference in New Issue
Block a user