start menu

This commit is contained in:
2023-01-21 12:13:59 +01:00
parent 8a93d29021
commit 1fdcc41801
6 changed files with 78 additions and 20 deletions
+21 -18
View File
@@ -1,5 +1,6 @@
#include "renderer.h"
#include "text.h"
#include "menu.h"
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_surface.h>
@@ -15,12 +16,12 @@ int main(void) {
}
Mix_Music *music = Mix_LoadMUS("./assets/music/pet.mp3");
if(Mix_PlayMusic(music, -1) != 0){
printf("Music sound could not be played!\n"
"SDL_Error: %s\n", Mix_GetError());
Mix_FreeMusic(music);
return 0;
}
// if(Mix_PlayMusic(music, -1) != 0){
// printf("Music sound could not be played!\n"
// "SDL_Error: %s\n", Mix_GetError());
// Mix_FreeMusic(music);
// return 0;
// }
RenderObject_t text;
SDL_Color white = {255, 255, 255, 255};
@@ -30,23 +31,25 @@ int main(void) {
exit(1);
}
SDL_Surface* img = IMG_Load("./assets/img/pp.png");
RenderObject_t duck = {
0.2,
0.2,
0,
0.50,
0.50,
0,
img
};
// SDL_Surface* img = IMG_Load("./assets/img/pp.png");
// RenderObject_t duck = {
// 0.2,
// 0.2,
// 0,
// 0.50,
// 0.50,
// 0,
// img
// };
MonitorSize_t monitorSize = renderer_getMonitorSize();
menu_render(monitorSize.width, monitorSize.height);
renderer_renderObject(text);
renderer_renderObject(duck);
// renderer_renderObject(duck);
while(1);
Mix_FreeMusic(music);
Mix_CloseAudio();
menu_remove();
text_freeFontCache();
}
+43
View File
@@ -0,0 +1,43 @@
#include "menu.h"
#include <SDL2/SDL_image.h>
static RenderId_t menuObjects[10];
static unsigned menuObjectsSize = 0;
int menu_render(int windowWidth, int windowHeight){
SDL_Surface* buttonImageSurface = IMG_Load("./assets/img/start.png");
if(buttonImageSurface == NULL){
printf("cannot load");
return -1;
}
float buttonScale = 0.3f;
// float buttonX = ((float)windowWidth - (float)buttonImageSurface->w) / (2.f * (float)buttonImageSurface->w);
// float buttonY = ((float)windowHeight - (float)buttonImageSurface->h) / (2.f * (float)buttonImageSurface->h);
float buttonX = 0.5f - buttonScale * (float)buttonImageSurface->w / (2.f * (float)windowWidth);
float buttonY = 0.5f - buttonScale * (float)buttonImageSurface->h / (2.f * (float)windowHeight);
printf("buttonImageSurface->w: %d\n", buttonImageSurface->w);
printf("buttonImageSurface->h: %d\n", buttonImageSurface->h);
printf("windowWidth: %d\n", windowWidth);
printf("buttonX: %f\n", buttonX);
RenderObject_t buttonImage = {
buttonX,
buttonY,
0,
buttonScale,
buttonScale,
0,
buttonImageSurface
};
menuObjects[menuObjectsSize] = renderer_renderObject(buttonImage);
++menuObjectsSize;
return 0;
}
void menu_remove(void){
for(unsigned i = 0; i < menuObjectsSize; ++i){
renderer_removeObject(menuObjects[i]);
}
menuObjectsSize = 0;
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef __MENU_H__
#define __MENU_H__
#include "renderer.h"
int menu_render(int windowWidth, int windowHeight);
void menu_remove(void);
#endif
+3 -2
View File
@@ -13,14 +13,15 @@ typedef struct FontCacheObject {
TTF_Font* font;
} FontCacheObject_t;
FontCacheObject_t fontCache[FONT_CACHE_SIZE];
unsigned fontCacheSize = 0;
static FontCacheObject_t fontCache[FONT_CACHE_SIZE];
static unsigned fontCacheSize = 0;
void text_freeFontCache(void){
for(unsigned i = 0; i < fontCacheSize; ++i){
free(fontCache[i].file);
TTF_CloseFont(fontCache[i].font);
}
fontCacheSize = 0;
}
int text_renderText(RenderObject_t* renderObject, float x, float y, int z, float scale, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...){