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
+1 -1
View File
@@ -14,7 +14,7 @@ static Mix_Music * music;
float accu = 0; float accu = 0;
void cinematic_staticRender(void) { void cinematic_staticRender(void) {
RenderObject_t AdamesLegend; RenderObject_t AdamesLegend = EMPTY_RENDER_OBJECT;
strcpy(AdamesLegend.name, "Adame's Legend"); strcpy(AdamesLegend.name, "Adame's Legend");
SDL_Color black = {255, 255, 255, 255}; SDL_Color black = {255, 255, 255, 255};
text_renderText(&AdamesLegend, 8, 0.8f, black, "./assets/fonts/Quinquefive.ttf", 40, "Adame est l'eleve modele de Polytech,\n Membre de plusieurs associations il est connu de tous.\n Lors de l'edition 21.2 de la Nantarena,\n Adame s'est retrouve a engeuler des joueurs pour leur demander de mettre leur putain de masque.\n\n L'edition finie, Adame a kidnappe l'ensemble des joueurs qui forcaient pour leur apprendre a respecter l'autorite a coup de coup de pied au cul."); text_renderText(&AdamesLegend, 8, 0.8f, black, "./assets/fonts/Quinquefive.ttf", 40, "Adame est l'eleve modele de Polytech,\n Membre de plusieurs associations il est connu de tous.\n Lors de l'edition 21.2 de la Nantarena,\n Adame s'est retrouve a engeuler des joueurs pour leur demander de mettre leur putain de masque.\n\n L'edition finie, Adame a kidnappe l'ensemble des joueurs qui forcaient pour leur apprendre a respecter l'autorite a coup de coup de pied au cul.");
+23 -23
View File
@@ -3,6 +3,7 @@
#include <SDL2/SDL_mixer.h> #include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_surface.h> #include <SDL2/SDL_surface.h>
#include "scaller.h" #include "scaller.h"
#include "scene.h"
#define SPEED 1.f/10000000000 #define SPEED 1.f/10000000000
#define GRAVITY 10000.f #define GRAVITY 10000.f
@@ -24,6 +25,8 @@ static float personPositionY = startPersonPositionY;
static const float personVelocityX = 200; static const float personVelocityX = 200;
static float personVelocityY = 0; static float personVelocityY = 0;
static RenderObject_t person;
static RenderId_t personId;
static void playMusic(){ static void playMusic(){
music = Mix_LoadMUS("./assets/music/jojo arab.mp3"); music = Mix_LoadMUS("./assets/music/jojo arab.mp3");
@@ -53,46 +56,43 @@ int game_staticRender(void){
} }
RenderObject_t gameBackground; RenderObject_t gameBackground = EMPTY_RENDER_OBJECT;
strcpy(gameBackground.name, "gameBackground"); strcpy(gameBackground.name, "gameBackground");
gameBackground.z = 0; gameBackground.z = 0;
gameBackground.surface = gameBackgroundSurface; gameBackground.surface = gameBackgroundSurface;
scaller(gameBackground.surface, 1, &gameBackground.width, &gameBackground.height); scaller(gameBackground.surface, 1, &gameBackground.width, &gameBackground.height);
gameBackground.height = 1; gameBackground.height = 1;
menuObjects[menuObjectsSize] = scene_addRenderObject(&gameScene, gameBackground, 0, 0); scene_addRenderObject(&gameScene, gameBackground, 0, 0);
renderer_setScene(&gameScene);
++menuObjectsSize;
return 0;
}
int game_render([[maybe_unused]] float delta){
for(unsigned i = 2; i < menuObjectsSize; ++i){
scene_removeRenderObject(&gameScene, menuObjects[i]);
}
menuObjectsSize = 2;
deltaAccu += delta;
SDL_Surface* buildingSurface = IMG_Load("./assets/img/building.png"); SDL_Surface* buildingSurface = IMG_Load("./assets/img/building.png");
RenderObject_t building; RenderObject_t building = EMPTY_RENDER_OBJECT;
strcpy(building.name, "building"); strcpy(building.name, "building");
building.z = 9; building.z = 9;
building.surface = buildingSurface; building.surface = buildingSurface;
building.width = 0.25; building.width = 0.25;
building.height = 1; building.height = 1;
menuObjects[menuObjectsSize] = scene_addRenderObject(&gameScene, building, 0, 0); scene_addRenderObject(&gameScene, building, 0, 0);
++menuObjectsSize;
SDL_Surface* personSurface = IMG_Load("./assets/img/person.png"); SDL_Surface* personSurface = IMG_Load("./assets/img/person.png");
RenderObject_t person;
strcpy(person.name, "person"); strcpy(person.name, "person");
person.z = 9; person.z = 9;
person.surface = personSurface; person.surface = personSurface;
person.width = 0.25f; person.width = 0.25f;
scaller(person.surface, 0.05f, &person.width, &person.height); scaller(person.surface, 0.05f, &person.width, &person.height);
personId = scene_addRenderObject(&gameScene, person, personPositionX, personPositionY);
return 0;
}
int game_render([[maybe_unused]] float delta){
//for(unsigned i = 0; i < menuObjectsSize; ++i){
// scene_removeRenderObject(&gameScene, menuObjects[i]);
//}
menuObjectsSize = 0;
deltaAccu += delta;
// if(!crashed && deltaAccu >= SPEED && isSpaceKeypressed){ // if(!crashed && deltaAccu >= SPEED && isSpaceKeypressed){
if(!crashed && isSpaceKeypressed){ if(!crashed && isSpaceKeypressed){
personPositionX += personVelocityX * delta; personPositionX += personVelocityX * delta;
@@ -103,8 +103,8 @@ int game_render([[maybe_unused]] float delta){
personPositionY += personVelocityY * delta; personPositionY += personVelocityY * delta;
if(personPositionY + CAMERA_HEIGHT * person.height >= CAMERA_HEIGHT - 2) crashed = true; if(personPositionY + CAMERA_HEIGHT * person.height >= CAMERA_HEIGHT - 2) crashed = true;
} }
menuObjects[menuObjectsSize] = scene_addRenderObject(&gameScene, person, personPositionX, personPositionY);
++menuObjectsSize; scene_moveRenderObject(&gameScene, personId, personPositionX, personPositionY);
return 0; return 0;
} }
+1 -1
View File
@@ -37,7 +37,7 @@ Scene_t demo;
static void game_setup(void) { static void game_setup(void) {
menu_staticRender(); menu_staticRender();
RenderObject_t text; RenderObject_t text = EMPTY_RENDER_OBJECT;
SDL_Color white = {255, 255, 255, 255}; SDL_Color white = {255, 255, 255, 255};
TTF_Init(); TTF_Init();
if(text_renderText(&text, 1000, 1, white, "./assets/fonts/arial.ttf", 25, "Hello World") < 0){ if(text_renderText(&text, 1000, 1, white, "./assets/fonts/arial.ttf", 25, "Hello World") < 0){
+5 -5
View File
@@ -45,7 +45,7 @@ int menu_staticRender(void){
running = false; running = false;
} }
RenderObject_t backgroundImage; RenderObject_t backgroundImage = EMPTY_RENDER_OBJECT;
strcpy(backgroundImage.name, "BackgroundImg"); strcpy(backgroundImage.name, "BackgroundImg");
backgroundImage.z = 0; backgroundImage.z = 0;
backgroundImage.surface = backgroundImageSurface; backgroundImage.surface = backgroundImageSurface;
@@ -54,14 +54,14 @@ int menu_staticRender(void){
menuObjects[menuObjectsSize] = scene_addRenderObject(&menuScene, backgroundImage, 0, 0); menuObjects[menuObjectsSize] = scene_addRenderObject(&menuScene, backgroundImage, 0, 0);
++menuObjectsSize; ++menuObjectsSize;
RenderObject_t legendOfAdam; RenderObject_t legendOfAdam = EMPTY_RENDER_OBJECT;
strcpy(legendOfAdam.name, "TheLegendOfA"); strcpy(legendOfAdam.name, "TheLegendOfA");
SDL_Color white = {255, 255, 255, 255}; SDL_Color white = {255, 255, 255, 255};
text_renderText(&legendOfAdam, 9, 1.f, white, "./assets/fonts/Quinquefive.ttf", 48, "The legend of adame"); text_renderText(&legendOfAdam, 9, 1.f, white, "./assets/fonts/Quinquefive.ttf", 48, "The legend of adame");
menuObjects[menuObjectsSize] = scene_addRenderObject(&menuScene, legendOfAdam, CAMERA_WIDTH * 0.095f, CAMERA_HEIGHT * 0.2f); menuObjects[menuObjectsSize] = scene_addRenderObject(&menuScene, legendOfAdam, CAMERA_WIDTH * 0.095f, CAMERA_HEIGHT * 0.2f);
++menuObjectsSize; ++menuObjectsSize;
RenderObject_t shadowOfAdam; RenderObject_t shadowOfAdam = EMPTY_RENDER_OBJECT;
strcpy(legendOfAdam.name, "shadowOfAdam"); strcpy(legendOfAdam.name, "shadowOfAdam");
SDL_Color black = {0, 0, 0, 255}; SDL_Color black = {0, 0, 0, 255};
text_renderText(&legendOfAdam, 8, 1.f, black, "./assets/fonts/Quinquefive.ttf", 48, "The legend of adame"); text_renderText(&legendOfAdam, 8, 1.f, black, "./assets/fonts/Quinquefive.ttf", 48, "The legend of adame");
@@ -91,14 +91,14 @@ int menu_render(float delta){
blinkDelta = 0; blinkDelta = 0;
} }
RenderObject_t startTextShadow; RenderObject_t startTextShadow = EMPTY_RENDER_OBJECT;
strcpy(startTextShadow.name, "StartTextShadow"); strcpy(startTextShadow.name, "StartTextShadow");
SDL_Color black = {0, 0, 0, textOpacity}; SDL_Color black = {0, 0, 0, textOpacity};
text_renderText(&startTextShadow, 9, 1.f, black, "./assets/fonts/Quinquefive.ttf", 28, "Press space to start"); text_renderText(&startTextShadow, 9, 1.f, black, "./assets/fonts/Quinquefive.ttf", 28, "Press space to start");
menuObjects[menuObjectsSize] = scene_addRenderObject(&menuScene, startTextShadow, CAMERA_WIDTH * 0.245f, CAMERA_HEIGHT * 0.855f); menuObjects[menuObjectsSize] = scene_addRenderObject(&menuScene, startTextShadow, CAMERA_WIDTH * 0.245f, CAMERA_HEIGHT * 0.855f);
++menuObjectsSize; ++menuObjectsSize;
RenderObject_t startText; RenderObject_t startText = EMPTY_RENDER_OBJECT;
strcpy(startText.name, "StartText"); strcpy(startText.name, "StartText");
SDL_Color orange = {255,165,0, textOpacity}; SDL_Color orange = {255,165,0, textOpacity};
text_renderText(&startText, 10, 1.f, orange, "./assets/fonts/Quinquefive.ttf", 28, "Press space to start"); text_renderText(&startText, 10, 1.f, orange, "./assets/fonts/Quinquefive.ttf", 28, "Press space to start");
+30 -2
View File
@@ -1,5 +1,6 @@
#include "renderer.h" #include "renderer.h"
#include "scene.h" #include "scene.h"
#include <SDL2/SDL_blendmode.h>
#include <SDL2/SDL_error.h> #include <SDL2/SDL_error.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
@@ -7,6 +8,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.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) { static int compartSort(const SceneObject_t * a, const SceneObject_t * b) {
return a->renderObj.z - b->renderObj.z; return a->renderObj.z - b->renderObj.z;
} }
@@ -15,6 +18,7 @@ void scene_render(const Scene_t * scene) {
MonitorSize_t monitorSize = renderer_getMonitorSize(); MonitorSize_t monitorSize = renderer_getMonitorSize();
for(int i = 0; i < scene->objectCount; i++) { 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]; const SceneObject_t * object = &scene->objects[i];
float x = (object->x - scene->x) / CAMERA_WIDTH; float x = (object->x - scene->x) / CAMERA_WIDTH;
float y = (object->y - scene->y) / CAMERA_HEIGHT; 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++; scene->objectCount++;
RenderId_t itemId = scene->lastId++; RenderId_t itemId = scene->lastId++;
printf("adding item: %s: %d\n", object.name, itemId);
if(scene->objectCount * sizeof(SceneObject_t) > scene->objectArraySize) { if(scene->objectCount * sizeof(SceneObject_t) > scene->objectArraySize) {
scene->objectArraySize = scene->objectCount * sizeof(SceneObject_t); scene->objectArraySize = scene->objectCount * sizeof(SceneObject_t);
scene->objects = realloc(scene->objects, scene->objectArraySize); 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; return itemId;
} }
void scene_removeRenderObject(Scene_t * scene, RenderId_t id) { static int indexof(Scene_t * scene, RenderId_t id) {
bool found = false; bool found = false;
int index = 0; int index = 0;
for(int i = 0; i < scene->objectCount; i++) { for(int i = 0; i < scene->objectCount; i++) {
@@ -85,8 +91,30 @@ void scene_removeRenderObject(Scene_t * scene, RenderId_t id) {
} }
if(!found){ if(!found){
printf("object %d not found\n", id); 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--; scene->objectCount--;
for(int i = index; i < scene->objectCount; i++) { for(int i = index; i < scene->objectCount; i++) {
+4
View File
@@ -6,6 +6,7 @@
#define CAMERA_WIDTH 160 #define CAMERA_WIDTH 160
#define CAMERA_HEIGHT 90 #define CAMERA_HEIGHT 90
typedef int RenderId_t; typedef int RenderId_t;
typedef struct RenderObject { typedef struct RenderObject {
@@ -31,8 +32,11 @@ typedef struct Scene {
float x,y; float x,y;
} Scene_t; } Scene_t;
extern const RenderObject_t EMPTY_RENDER_OBJECT;
void scene_render(const Scene_t * scene); void scene_render(const Scene_t * scene);
void scene_removeRenderObject(Scene_t * scene, RenderId_t id); void scene_removeRenderObject(Scene_t * scene, RenderId_t id);
void scene_moveRenderObject(Scene_t * scene, RenderId_t id, float x, float y);
RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x, int y); RenderId_t scene_addRenderObject(Scene_t * scene, RenderObject_t object, int x, int y);
void scene_free(Scene_t * scene); void scene_free(Scene_t * scene);