Plus jammais on en reparle
This commit is contained in:
+31
-19
@@ -4,29 +4,27 @@
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_surface.h>
|
||||
#include <stdio.h>
|
||||
#include <string.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) {
|
||||
void scene_render(const Scene_t * scene) {
|
||||
MonitorSize_t monitorSize = renderer_getMonitorSize();
|
||||
int minScanX = scene->x - CAMERA_WIDTH;
|
||||
int minScanY = scene->y - CAMERA_HEIGHT;
|
||||
int maxScanX = scene->x + CAMERA_WIDTH;
|
||||
int maxScanY = scene->y + CAMERA_HEIGHT;
|
||||
|
||||
printf("camera: %d %d %d %d\n", minScanX, minScanY, maxScanX, maxScanY);
|
||||
|
||||
for(int i = 0; i < scene->objectCount; i++) {
|
||||
SceneObject_t * object = &scene->objects[i];
|
||||
const SceneObject_t * object = &scene->objects[i];
|
||||
if(object->x > minScanX && object->x < maxScanX && object->y > minScanY && object->y < maxScanY) {
|
||||
float x = (object->x) / CAMERA_WIDTH;
|
||||
float y = (object->y) / CAMERA_HEIGHT;
|
||||
//the object is to be rendered
|
||||
|
||||
printf("%f %f\n", x, y);
|
||||
|
||||
SDL_Rect rect = {
|
||||
round(x * monitorSize.width) ,
|
||||
round(y * monitorSize.height),
|
||||
@@ -54,9 +52,16 @@ static void buildCache(SceneObject_t * object) {
|
||||
}
|
||||
|
||||
RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x, int y) {
|
||||
RenderId_t itemId = scene->objectCount++;
|
||||
printf("Adding: %s\n", object.name);
|
||||
scene->objects = realloc(scene->objects, scene->objectCount * sizeof(SceneObject_t));
|
||||
scene->objectCount++;
|
||||
RenderId_t itemId = scene->lastId++;
|
||||
|
||||
if(scene->objectCount * sizeof(SceneObject_t) > scene->objectArraySize) {
|
||||
scene->objectArraySize = scene->objectCount * sizeof(SceneObject_t);
|
||||
scene->objects = realloc(scene->objects, scene->objectArraySize);
|
||||
if(scene->objects == NULL) {
|
||||
printf("Realloc failed\n");
|
||||
}
|
||||
}
|
||||
|
||||
SceneObject_t item = {
|
||||
x, y,
|
||||
@@ -67,7 +72,8 @@ RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x,
|
||||
|
||||
buildCache(&item);
|
||||
|
||||
scene->objects[itemId] = item;
|
||||
scene->objects[scene->objectCount - 1] = item;
|
||||
|
||||
//the objects are sorted
|
||||
qsort(scene->objects, scene->objectCount, sizeof(SceneObject_t), (__compar_fn_t)compartSort);
|
||||
return itemId;
|
||||
@@ -75,18 +81,23 @@ RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x,
|
||||
|
||||
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];
|
||||
int index = 0;
|
||||
for(int i = 0; i < scene->objectCount; i++) {
|
||||
if(scene->objects[i].id == id){
|
||||
found = true;
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!found){
|
||||
printf("object %d not found\n", id);
|
||||
return;
|
||||
}
|
||||
scene->objectCount--;
|
||||
|
||||
for(int i = index; i < scene->objectCount; i++) {
|
||||
scene->objects[i] = scene->objects[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
void scene_free(Scene_t * scene) {
|
||||
@@ -97,6 +108,7 @@ void scene_free(Scene_t * scene) {
|
||||
}
|
||||
}
|
||||
free(scene->objects);
|
||||
scene->objectArraySize = 0;
|
||||
scene->objectCount = 0;
|
||||
scene->objects = NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user