Render fixes + performance boost

This commit is contained in:
Marc
2023-01-22 13:56:44 +01:00
parent b7ca43006a
commit c0404ae69d
6 changed files with 64 additions and 32 deletions
+30 -2
View File
@@ -1,5 +1,6 @@
#include "renderer.h"
#include "scene.h"
#include <SDL2/SDL_blendmode.h>
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h>
@@ -7,6 +8,8 @@
#include <stdio.h>
#include <string.h>
const RenderObject_t EMPTY_RENDER_OBJECT = { 0, 0, 0, 0, NULL, "Undefined" };
static int compartSort(const SceneObject_t * a, const SceneObject_t * b) {
return a->renderObj.z - b->renderObj.z;
}
@@ -15,6 +18,7 @@ void scene_render(const Scene_t * scene) {
MonitorSize_t monitorSize = renderer_getMonitorSize();
for(int i = 0; i < scene->objectCount; i++) {
printf("Rendering %d, %s\n", scene->objects[i].id, scene->objects[i].renderObj.name);
const SceneObject_t * object = &scene->objects[i];
float x = (object->x - scene->x) / CAMERA_WIDTH;
float y = (object->y - scene->y) / CAMERA_HEIGHT;
@@ -49,6 +53,8 @@ RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x,
scene->objectCount++;
RenderId_t itemId = scene->lastId++;
printf("adding item: %s: %d\n", object.name, itemId);
if(scene->objectCount * sizeof(SceneObject_t) > scene->objectArraySize) {
scene->objectArraySize = scene->objectCount * sizeof(SceneObject_t);
scene->objects = realloc(scene->objects, scene->objectArraySize);
@@ -73,7 +79,7 @@ RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x,
return itemId;
}
void scene_removeRenderObject(Scene_t * scene, RenderId_t id) {
static int indexof(Scene_t * scene, RenderId_t id) {
bool found = false;
int index = 0;
for(int i = 0; i < scene->objectCount; i++) {
@@ -85,8 +91,30 @@ void scene_removeRenderObject(Scene_t * scene, RenderId_t id) {
}
if(!found){
printf("object %d not found\n", id);
return;
return -1;
}
return index;
}
void scene_moveRenderObject(Scene_t * scene, RenderId_t id, float x, float y) {
int index = indexof(scene, id);
if(index < 0)
return;
SceneObject_t * object = &scene->objects[index];
object->x = x;
object->y = y;
}
void scene_removeRenderObject(Scene_t * scene, RenderId_t id) {
int index = indexof(scene, id);
if(index < 0)
return;
printf("removing item: %s: %d\n", scene->objects[index].renderObj.name, id);
scene->objectCount--;
for(int i = index; i < scene->objectCount; i++) {