start menu
This commit is contained in:
@@ -23,6 +23,7 @@ set(SRC
|
|||||||
src/main.c
|
src/main.c
|
||||||
src/renderer.c
|
src/renderer.c
|
||||||
src/text.c
|
src/text.c
|
||||||
|
src/menu.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# add the executable
|
# add the executable
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
+21
-18
@@ -1,5 +1,6 @@
|
|||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
|
#include "menu.h"
|
||||||
#include <SDL2/SDL_mixer.h>
|
#include <SDL2/SDL_mixer.h>
|
||||||
#include <SDL2/SDL_image.h>
|
#include <SDL2/SDL_image.h>
|
||||||
#include <SDL2/SDL_surface.h>
|
#include <SDL2/SDL_surface.h>
|
||||||
@@ -15,12 +16,12 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Mix_Music *music = Mix_LoadMUS("./assets/music/pet.mp3");
|
Mix_Music *music = Mix_LoadMUS("./assets/music/pet.mp3");
|
||||||
if(Mix_PlayMusic(music, -1) != 0){
|
// if(Mix_PlayMusic(music, -1) != 0){
|
||||||
printf("Music sound could not be played!\n"
|
// printf("Music sound could not be played!\n"
|
||||||
"SDL_Error: %s\n", Mix_GetError());
|
// "SDL_Error: %s\n", Mix_GetError());
|
||||||
Mix_FreeMusic(music);
|
// Mix_FreeMusic(music);
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
// }
|
||||||
|
|
||||||
RenderObject_t text;
|
RenderObject_t text;
|
||||||
SDL_Color white = {255, 255, 255, 255};
|
SDL_Color white = {255, 255, 255, 255};
|
||||||
@@ -30,23 +31,25 @@ int main(void) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Surface* img = IMG_Load("./assets/img/pp.png");
|
// SDL_Surface* img = IMG_Load("./assets/img/pp.png");
|
||||||
|
|
||||||
RenderObject_t duck = {
|
|
||||||
0.2,
|
|
||||||
0.2,
|
|
||||||
0,
|
|
||||||
0.50,
|
|
||||||
0.50,
|
|
||||||
0,
|
|
||||||
img
|
|
||||||
};
|
|
||||||
|
|
||||||
|
// 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(text);
|
||||||
renderer_renderObject(duck);
|
// renderer_renderObject(duck);
|
||||||
while(1);
|
while(1);
|
||||||
Mix_FreeMusic(music);
|
Mix_FreeMusic(music);
|
||||||
Mix_CloseAudio();
|
Mix_CloseAudio();
|
||||||
|
menu_remove();
|
||||||
|
|
||||||
text_freeFontCache();
|
text_freeFontCache();
|
||||||
}
|
}
|
||||||
|
|||||||
+43
@@ -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
@@ -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
@@ -13,14 +13,15 @@ typedef struct FontCacheObject {
|
|||||||
TTF_Font* font;
|
TTF_Font* font;
|
||||||
} FontCacheObject_t;
|
} FontCacheObject_t;
|
||||||
|
|
||||||
FontCacheObject_t fontCache[FONT_CACHE_SIZE];
|
static FontCacheObject_t fontCache[FONT_CACHE_SIZE];
|
||||||
unsigned fontCacheSize = 0;
|
static unsigned fontCacheSize = 0;
|
||||||
|
|
||||||
void text_freeFontCache(void){
|
void text_freeFontCache(void){
|
||||||
for(unsigned i = 0; i < fontCacheSize; ++i){
|
for(unsigned i = 0; i < fontCacheSize; ++i){
|
||||||
free(fontCache[i].file);
|
free(fontCache[i].file);
|
||||||
TTF_CloseFont(fontCache[i].font);
|
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, ...){
|
int text_renderText(RenderObject_t* renderObject, float x, float y, int z, float scale, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...){
|
||||||
|
|||||||
Reference in New Issue
Block a user