winter is comming: brace yourself
This commit is contained in:
+80
@@ -0,0 +1,80 @@
|
||||
#include "renderer.h"
|
||||
#include "scene.h"
|
||||
|
||||
static int compartSort(const SceneObject_t * a, const SceneObject_t * b) {
|
||||
return a->renderObj.z - b->renderObj.z;
|
||||
}
|
||||
|
||||
void scene_render(Scene_t * scene) {
|
||||
MonitorSize_t monitorSize = renderer_getMonitorSize();
|
||||
int minX = scene->x - CAMERA_WIDTH/2;
|
||||
int minY = scene->y - CAMERA_HEIGHT/2;
|
||||
int maxX = scene->x - CAMERA_WIDTH/2;
|
||||
int maxY = scene->y - CAMERA_HEIGHT/2;
|
||||
|
||||
for(int i = 0; i < scene->objectCount; i++) {
|
||||
SceneObject_t * object = &scene->objects[i];
|
||||
if(object->x > minX && object->x < maxX && object->y > minY && object->y < maxY) {
|
||||
int x = (object->x - minX) / CAMERA_WIDTH;
|
||||
int y = (object->y - minY) / CAMERA_HEIGHT;
|
||||
//the object is to be rendered
|
||||
|
||||
SDL_Rect rect = {
|
||||
round(x * monitorSize.width) ,
|
||||
round(y * monitorSize.height),
|
||||
round(object->renderObj.width * monitorSize.width),
|
||||
round(object->renderObj.height * monitorSize.height)
|
||||
};
|
||||
|
||||
int error = SDL_RenderCopyEx(renderer, object->cache, NULL, &rect, object->renderObj.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void buildCache(SceneObject_t * object) {
|
||||
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, object->renderObj.surface);
|
||||
if(texture == NULL) {
|
||||
fprintf(stderr, "Error while creating texture %s is_null: %d\n", SDL_GetError(), object->renderObj.surface == NULL);
|
||||
exit(1);
|
||||
}
|
||||
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
|
||||
object->cache = texture;
|
||||
}
|
||||
|
||||
RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x, int y) {
|
||||
RenderId_t itemId = scene->objectCount++;
|
||||
|
||||
SceneObject_t item = {
|
||||
x, y,
|
||||
object,
|
||||
NULL,
|
||||
itemId
|
||||
};
|
||||
|
||||
buildCache(&item);
|
||||
|
||||
scene->objects[itemId] = item;
|
||||
//the objects are sorted
|
||||
qsort(scene->objects, scene->objectCount, sizeof(RenderObject_t), (__compar_fn_t)compartSort);
|
||||
return itemId;
|
||||
}
|
||||
|
||||
void scene_removeRenderObject(Scene_t * scene, RenderId_t id) {
|
||||
bool found = false;
|
||||
for(int i = 0; i < scene->objectCount - 1; i++) {
|
||||
if(scene->objects[i].id == id) {
|
||||
SDL_FreeSurface(scene->objects[i].renderObj.surface);
|
||||
if(scene->objects[i].cache != NULL) {
|
||||
SDL_DestroyTexture(scene->objects[i].cache);
|
||||
}
|
||||
}
|
||||
if(found) {
|
||||
scene->objects[i] = scene->objects[i + 1];
|
||||
}
|
||||
}
|
||||
scene->objectCount--;
|
||||
}
|
||||
Reference in New Issue
Block a user