Sa marche !!!!

This commit is contained in:
Marc
2023-01-21 16:12:51 +01:00
parent bf91cf1237
commit 814d135ada
3 changed files with 26 additions and 12 deletions
+17 -8
View File
@@ -1,5 +1,9 @@
#include "renderer.h"
#include "scene.h"
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
static int compartSort(const SceneObject_t * a, const SceneObject_t * b) {
return a->renderObj.z - b->renderObj.z;
@@ -7,18 +11,22 @@ static int compartSort(const SceneObject_t * a, const SceneObject_t * b) {
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;
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];
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;
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),
@@ -47,6 +55,7 @@ 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));
SceneObject_t item = {
@@ -60,7 +69,7 @@ RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x,
scene->objects[itemId] = item;
//the objects are sorted
qsort(scene->objects, scene->objectCount, sizeof(RenderObject_t), (__compar_fn_t)compartSort);
qsort(scene->objects, scene->objectCount, sizeof(SceneObject_t), (__compar_fn_t)compartSort);
return itemId;
}