Filenames as defines

This commit is contained in:
Michael Chen 2018-01-16 12:10:37 +01:00
parent 8dbdf176f3
commit 7e9eaf5861
6 changed files with 59 additions and 19 deletions

View File

@ -10,6 +10,10 @@
#include "breakout.h"
#include "vector.h"
#define BALL_TexturePath "assets/images/ball.png"
#define PADDLE_TexturePath "assets/images/paddle.png"
#define BLOCK_TexturePath "assets/images/spritesheet.png"
#ifndef __nullptr__
#define Nullptr(type) (type *)0
#endif // __nullptr__
@ -56,10 +60,14 @@ Scenery BREAKOUT_CreateDefault(){
scenery.blocks[x + index] = BLOCK_CreateDefault();
scenery.blocks[x + index].TargetRect = (SDL_Rect) {.x = ((192 * x) + 4), .y = ((96 * y) + 2), .w = 184, .h = 92 };
scenery.blocks[x + index].TextureIndex = y + x + 2;
// printf("Block created at index: %d\n", (x + index));
// printf("Block Target: %d %d %d %d\n", ( scenery.blocks[x + index].TargetRect.x), ( scenery.blocks[x + index].TargetRect.y), ( scenery.blocks[x + index].TargetRect.w), ( scenery.blocks[x + index].TargetRect.h));
// printf("Block Texture index: %d\n", scenery.blocks[x + index].TextureIndex);
// system("pause");
}
}
return scenery;
}
} /* BREAKOUT_CreateDefault */
// This Function is obsolete! Do not use it!
void BREAKOUT_ChangeSize(int width, int height){
@ -111,7 +119,7 @@ void BREAKOUT_DestroyObject(Scenery * scenery){
void BALL_Initialize(SDL_Renderer * renderer){
if (!BALL_IsInit) {
printf("Initializing Ball...\n");
BALL_Texture = IMG_LoadTexture(renderer, "assets/images/ball.png");
BALL_Texture = IMG_LoadTexture(renderer, BALL_TexturePath);
if (!BALL_Texture) printf("Ball texture failed to load!\n");
BALL_SourceRects = (SDL_Rect *)malloc(1 * sizeof(SDL_Rect));
if (!BALL_SourceRects) printf("FATAL! Memory allocation failed!\n");
@ -280,7 +288,7 @@ void BALL_Deinitialize(){
void PADDLE_Initialize(SDL_Renderer * renderer){
if (!PADDLE_IsInit) {
printf("Initializing Paddle...\n");
PADDLE_Texture = IMG_LoadTexture(renderer, "assets/images/paddle.png");
PADDLE_Texture = IMG_LoadTexture(renderer, PADDLE_TexturePath);
if (!PADDLE_Texture) printf("Paddle texture failed to load!\n");
PADDLE_SourceRects = (SDL_Rect *)malloc(1 * sizeof(SDL_Rect));
if (!PADDLE_SourceRects) printf("FATAL! Memory allocation failed!\n");
@ -365,7 +373,7 @@ void PADDLE_Deinitialize(){
void BLOCK_Initialize(SDL_Renderer * renderer){
if (!BLOCK_IsInit) {
printf("Initializing Block...\n");
BLOCK_Texture = IMG_LoadTexture(renderer, "assets/images/spritesheet.png");
BLOCK_Texture = IMG_LoadTexture(renderer, BLOCK_TexturePath);
if (!BLOCK_Texture) printf("Block texture failed to load!\n");
BLOCK_SourceRects = (SDL_Rect *)malloc(BLOCK_TextureCount * sizeof(SDL_Rect));
if (!BLOCK_SourceRects) printf("FATAL! Memory allocation failed!\n");

0
button.c Normal file
View File

14
button.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef __button_h__
#define __button_h__
// Structs
typedef struct buttonStruct {
SDL_Rect TargetRect;
void (*OnClick)();
};
// End Structs
// Prototypes
// End Prototypes
#endif

View File

@ -8,6 +8,8 @@
#include "highscores.h"
#define HIGHSCORES_FontFamily "assets/fonts/monofur.ttf"
int entriesGot = 0;
User * ul;
SDL_Color White;
@ -21,11 +23,10 @@ void HIGHSCORES_Initialize(){
Message_rect = (SDL_Rect) {.x = 0, .y = 0, .w = 2, .h = 2 };
White = (SDL_Color) {255, 255, 255 };
ul = malloc(10 * sizeof(User));
font = TTF_OpenFont("assets/fonts/monofur.ttf", 48);
font = TTF_OpenFont(HIGHSCORES_FontFamily, 48);
if (!font) printf("Font could not initialize! Error: %s\n", TTF_GetError());
else printf("Font was successfully initialized!\n");
printFontStyle(font);
HIGHSCORES_ReloadList(&entriesGot);
printf("Highscores initialized!\n");
}
@ -52,7 +53,7 @@ void printFontStyle(TTF_Font * ffont){
void HIGHSCORES_Draw(SDL_Renderer * renderer){
char * buffer = calloc(100, sizeof(char));
int count = 0;
char format[20] = "| %-50s | %-10s |";
char format[20] = "| %-58s | %-10s |";
sprintf(buffer, format, "Username", "Score");
HIGHSCORES_GenerateTexture(renderer, buffer);
@ -63,7 +64,7 @@ void HIGHSCORES_Draw(SDL_Renderer * renderer){
while (count < entriesGot) {
sprintf(buffer, format, ul[count].Username, ul[count].Score);
HIGHSCORES_GenerateTexture(renderer, buffer);
Message_rect.y = ((Message_rect.h + 10) * (count + 1)) + 140;
Message_rect.y = ((Message_rect.h + 15) * (count + 1)) + 140;
Message_rect.x = 50;
SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
SDL_DestroyTexture(Message);
@ -92,12 +93,12 @@ void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer, char * text){
Message_rect.h = h;
}
void HIGHSCORES_ReloadList(int * usercount){
void HIGHSCORES_ReloadList(){
printf("Call BHI interface:\n");
system("bhi top output.txt");
printf("BHI interface quit!\nBHI output handling...\n");
*usercount = 0;
entriesGot = 0;
FILE * fp;
char * line = NULL;
size_t len = 0;
@ -151,6 +152,6 @@ void HIGHSCORES_ReloadList(int * usercount){
printf("User: %s -> Score: %s\n", ul[i].Username, ul[i].Score);
}
*usercount = counter;
entriesGot = counter;
printf("BHI Interface successfully quit!\n");
} /* main */

View File

@ -13,7 +13,7 @@ void printFontStyle(TTF_Font * ffont);
void HIGHSCORES_Draw(SDL_Renderer * renderer);
void HIGHSCORES_Deinitialize();
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer, char * text);
void HIGHSCORES_ReloadList(int * usercount);
void HIGHSCORES_ReloadList();
// End Prototypes
#endif

31
main.c
View File

@ -22,21 +22,24 @@
#define ue "\201"
#define ss "\341"
void INITIALIZE();
void QUIT();
// Prototypes
void GAME_ChangeState(GameState state);
void HandleSDLEvents();
void DrawBackground(SDL_Renderer * renderer);
void printFontStyle(TTF_Font * ffont);
void mousePress(SDL_MouseButtonEvent b);
void keyPress(SDL_KeyboardEvent b);
void windowChanged(SDL_WindowEvent b);
void toggleFullscreen();
void windowChanged(SDL_WindowEvent b);
void DrawBackground(SDL_Renderer * renderer);
void INITIALIZE();
void QUIT();
int readIntFromIO(char * m1, char * m2, char * m3, int min, int max);
// End Prototypes
// Default Render Size (Upscaled for bigger monitors)
const int width = 1920;
const int height = 1080;
float XScale = 1.0f, YScale = 1.0f;
// End render properties
int numKeys;
const Uint8 * keystate; // TODO: export all this into scenery and enemy waves
@ -49,7 +52,7 @@ Scenery scenery;
int main(int argc, char * args[]){
printf("Spielbereiche\n\t- 1: Hauptmen"ue "\n\t- 2: Spiel\n\t- 3: Level Select\n\t- 4: Settings\n\t- 5: Highscores\n");
gameState = readIntFromIO("W"ae "hle einen Spielbereich aus, den du testen m"oe "chtest:", "Fehlerhafte Eingabe!\n", "%d ist kein g"ue "ltiger Spielbereich!\n", 1, 5);
GAME_ChangeState(readIntFromIO("W"ae "hle einen Spielbereich aus, den du testen m"oe "chtest:", "Fehlerhafte Eingabe!\n", "%d ist kein g"ue "ltiger Spielbereich!\n", 1, 5));
INITIALIZE();
while (running) { // Gameloop
HandleSDLEvents();
@ -68,6 +71,7 @@ int main(int argc, char * args[]){
break;
default:
printf("Unknow state was updated: %d\n", gameState);
break;
}
// DrawText(renderer, "FICK DICH");
SDL_RenderPresent(renderer);
@ -76,6 +80,19 @@ int main(int argc, char * args[]){
return 0;
} /* main */
void GAME_ChangeState(GameState state){
gameState = state;
switch (gameState) {
case Highscores:
HIGHSCORES_ReloadList();
printf("State was changed to Highscores!\n");
break;
default:
printf("State was changed to %d!\n", gameState);
break;
}
}
void HandleSDLEvents(){
while (SDL_PollEvent(&event)) {
switch (event.type) {