Renderer
This commit is contained in:
+113
@@ -0,0 +1,113 @@
|
||||
#include "renderer.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_blendmode.h>
|
||||
#include <SDL2/SDL_rect.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_timer.h>
|
||||
#include <bits/pthreadtypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
#include <pthread.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;
|
||||
SDL_Window* win;
|
||||
|
||||
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 *) {
|
||||
SDL_GetRendererOutputSize(renderer, &monitorSize.width, &monitorSize.height);
|
||||
|
||||
objectStorageSize = 1000;
|
||||
objects = malloc(objectStorageSize);
|
||||
|
||||
while(true) {
|
||||
for(int i = 0; i < objectCount; i++) {
|
||||
RenderItem_t * object = &objects[i];
|
||||
if(object->cache == NULL) {
|
||||
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, object->object.surface);
|
||||
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
|
||||
object->cache = texture;
|
||||
}
|
||||
SDL_RenderCopyEx(renderer, object->cache, &object->rect, NULL, object->object.angle_degre, NULL, SDL_FLIP_NONE);
|
||||
}
|
||||
SDL_Delay(1000 / 400); //400fps or u a pussy
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void renderer_main() {
|
||||
// returns zero on success else non-zero
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||
printf("error initializing SDL: %s\n", SDL_GetError());
|
||||
}
|
||||
win = SDL_CreateWindow("GAME", // creates a window
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
1000, 1000, 0);
|
||||
|
||||
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
|
||||
|
||||
// creates a renderer to render our images
|
||||
renderer = SDL_CreateRenderer(win, -1, render_flags);
|
||||
|
||||
pthread_create(&thread, NULL, render_thread, NULL);
|
||||
}
|
||||
|
||||
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.height * monitorSize.height),
|
||||
round(object.width * monitorSize.width)
|
||||
};
|
||||
|
||||
RenderItem_t item = {
|
||||
object,
|
||||
NULL,
|
||||
rect,
|
||||
itemId
|
||||
};
|
||||
|
||||
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