Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
bf6b884523 |
26
breakout.c
26
breakout.c
@ -9,6 +9,7 @@
|
||||
|
||||
#include "breakout.h"
|
||||
#include "vector.h"
|
||||
#include "font.h"
|
||||
|
||||
extern float XScale, YScale;
|
||||
|
||||
@ -79,14 +80,10 @@ void BREAKOUT_ChangeSize(int width, int height){
|
||||
}
|
||||
|
||||
void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate){
|
||||
if (scenery->IsPaused) {
|
||||
// Render "Paused"
|
||||
return;
|
||||
}
|
||||
if ((scenery->StartCountdown)-- > 0) {
|
||||
// Render "Countdown"
|
||||
return;
|
||||
}
|
||||
if (scenery->IsPaused) return; // Currently paused
|
||||
if ((scenery->StartCountdown)-- < 0)
|
||||
(scenery->StartCountdown) = 0;
|
||||
else return; // Currently Counting down
|
||||
if (scenery->IsGameOver) {
|
||||
BALL_ResetPosition(&(scenery->ball));
|
||||
PADDLE_ResetPosition(&(scenery->paddle));
|
||||
@ -111,6 +108,19 @@ void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer){
|
||||
}
|
||||
BALL_Draw(renderer, &(scenery->ball));
|
||||
PADDLE_Draw(renderer, &(scenery->paddle));
|
||||
if (scenery->IsPaused)
|
||||
FONT_RenderTextCentered(renderer, "PAUSED", 3.0f, 1);
|
||||
else if ((scenery->StartCountdown) > 0) {
|
||||
if ((scenery->StartCountdown) <= 60) {
|
||||
FONT_RenderTextCentered(renderer, "GO", 6.0f, 3);
|
||||
} else if ((scenery->StartCountdown) <= 120) {
|
||||
FONT_RenderTextCentered(renderer, "1", 6.0f, 1);
|
||||
} else if ((scenery->StartCountdown) <= 180) {
|
||||
FONT_RenderTextCentered(renderer, "2", 6.0f, 1);
|
||||
} else {
|
||||
FONT_RenderTextCentered(renderer, "3", 6.0f, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BREAKOUT_DEINITIALIZE(){
|
||||
|
88
font.c
88
font.c
@ -1,25 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
|
||||
#include "font.h"
|
||||
|
||||
#define FONT_FontFile1 "assets/fonts/monofur.ttf"
|
||||
#define FONT_FontFile2 "assets/fonts/ka1.ttf"
|
||||
#define FONT_FontFile3 "assets/fonts/minecraft.ttf"
|
||||
#define FONT_FontFile4 "assets/fonts/ae-bi.ttf"
|
||||
|
||||
extern int width, height;
|
||||
|
||||
int FONT_FontCount;
|
||||
SDL_Color FONT_FontColor;
|
||||
TTF_Font * FONT_FontFamily = NULL;
|
||||
TTF_Font ** FONT_FontFamily;
|
||||
bool FONT_IsInit = false;
|
||||
|
||||
void FONT_Initialize(){
|
||||
printf("Initializing Font...\n");
|
||||
FONT_FontColor = (SDL_Color) {255, 255, 255 };
|
||||
FONT_FontFamily = TTF_OpenFont(FONT_FontFile, 48);
|
||||
if (!FONT_FontFamily) printf("Font could not initialize! Error: %s\n", TTF_GetError());
|
||||
else printf("Font was successfully initialized!\n");
|
||||
FONT_PrintFontStyle(FONT_FontFamily);
|
||||
printf("Font initialized!\n");
|
||||
}
|
||||
if (!FONT_IsInit) {
|
||||
printf("Initializing Font...\n");
|
||||
FONT_FontColor = (SDL_Color) {255, 255, 255 };
|
||||
FONT_FontCount = 4;
|
||||
FONT_FontFamily = (TTF_Font **)malloc(FONT_FontCount * sizeof(TTF_Font *));
|
||||
FONT_FontFamily[0] = TTF_OpenFont(FONT_FontFile1, 48);
|
||||
if (!FONT_FontFamily) printf("Font 1 could not initialize! Error: %s\n", TTF_GetError());
|
||||
else printf("Font 1 was successfully initialized!\n");
|
||||
FONT_PrintFontStyle(FONT_FontFamily[0]);
|
||||
FONT_FontFamily[1] = TTF_OpenFont(FONT_FontFile2, 48);
|
||||
if (!FONT_FontFamily) printf("Font 2 could not initialize! Error: %s\n", TTF_GetError());
|
||||
else printf("Font 2 was successfully initialized!\n");
|
||||
FONT_PrintFontStyle(FONT_FontFamily[1]);
|
||||
FONT_FontFamily[2] = TTF_OpenFont(FONT_FontFile3, 48);
|
||||
if (!FONT_FontFamily) printf("Font 3 could not initialize! Error: %s\n", TTF_GetError());
|
||||
else printf("Font 3 was successfully initialized!\n");
|
||||
FONT_PrintFontStyle(FONT_FontFamily[2]);
|
||||
FONT_FontFamily[3] = TTF_OpenFont(FONT_FontFile4, 48);
|
||||
if (!FONT_FontFamily) printf("Font 4 could not initialize! Error: %s\n", TTF_GetError());
|
||||
else printf("Font 4 was successfully initialized!\n");
|
||||
FONT_PrintFontStyle(FONT_FontFamily[3]);
|
||||
printf("Font initialized!\n");
|
||||
FONT_IsInit = true;
|
||||
} else printf("Font already initialized!\n");
|
||||
} /* FONT_Initialize */
|
||||
|
||||
void FONT_PrintFontStyle(TTF_Font * ffont){
|
||||
int style;
|
||||
@ -42,31 +68,51 @@ void FONT_PrintFontStyle(TTF_Font * ffont){
|
||||
}
|
||||
|
||||
void FONT_Deinitialize(){
|
||||
printf("De-initializing Font...\n");
|
||||
TTF_CloseFont(FONT_FontFamily);
|
||||
FONT_FontFamily = NULL; // to be safe...
|
||||
printf("Font de-initialized!\n");
|
||||
if (FONT_IsInit) {
|
||||
printf("De-initializing Font...\n");
|
||||
for (int i = 0; i < FONT_FontCount; i++) {
|
||||
TTF_CloseFont(FONT_FontFamily[i]);
|
||||
}
|
||||
free(FONT_FontFamily);
|
||||
printf("Font de-initialized!\n");
|
||||
FONT_IsInit = false;
|
||||
} else printf("Font already de-initialized!\n");
|
||||
}
|
||||
|
||||
void FONT_RenderText(SDL_Renderer * renderer, char * text, SDL_Rect * dstRect){
|
||||
void FONT_RenderText(SDL_Renderer * renderer, char * text, SDL_Rect * dstRect, int index){
|
||||
SDL_Rect srcRect;
|
||||
|
||||
srcRect.x = 0;
|
||||
srcRect.y = 0;
|
||||
SDL_Texture * texture = FONT_GenerateTexture(renderer, text, &srcRect);
|
||||
SDL_Texture * texture = FONT_GenerateTexture(renderer, text, &srcRect, index);
|
||||
SDL_RenderCopy(renderer, texture, &srcRect, dstRect);
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
|
||||
SDL_Texture * FONT_GenerateTexture(SDL_Renderer * renderer, char * text, SDL_Rect * Message_rect){
|
||||
SDL_Surface * tmpSurface = FONT_GenerateSurface(text, Message_rect);
|
||||
void FONT_RenderTextCentered(SDL_Renderer * renderer, char * text, float scale, int index){
|
||||
SDL_Rect srcRect, dstRect;
|
||||
|
||||
srcRect.x = 0;
|
||||
srcRect.y = 0;
|
||||
|
||||
SDL_Texture * texture = FONT_GenerateTexture(renderer, text, &srcRect, index);
|
||||
dstRect.w = (int)roundf((float)(srcRect.w) * scale);
|
||||
dstRect.h = (int)roundf((float)(srcRect.h) * scale);
|
||||
dstRect.x = (width - dstRect.w) / 2;
|
||||
dstRect.y = (height - dstRect.h) / 2;
|
||||
SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
|
||||
SDL_Texture * FONT_GenerateTexture(SDL_Renderer * renderer, char * text, SDL_Rect * Message_rect, int index){
|
||||
SDL_Surface * tmpSurface = FONT_GenerateSurface(text, Message_rect, index);
|
||||
SDL_Texture * resultTexture = SDL_CreateTextureFromSurface(renderer, tmpSurface);
|
||||
|
||||
SDL_FreeSurface(tmpSurface);
|
||||
return resultTexture;
|
||||
} /* FONT_GenerateSurface */
|
||||
|
||||
SDL_Surface * FONT_GenerateSurface(char * text, SDL_Rect * Message_rect){
|
||||
TTF_SizeText(FONT_FontFamily, text, &(Message_rect->w), &(Message_rect->h));
|
||||
return TTF_RenderText_Solid(FONT_FontFamily, text, FONT_FontColor);
|
||||
SDL_Surface * FONT_GenerateSurface(char * text, SDL_Rect * Message_rect, int index){
|
||||
TTF_SizeText(FONT_FontFamily[index], text, &(Message_rect->w), &(Message_rect->h));
|
||||
return TTF_RenderText_Solid(FONT_FontFamily[index], text, FONT_FontColor);
|
||||
}
|
||||
|
14
font.h
14
font.h
@ -1,20 +1,14 @@
|
||||
#ifndef __font_h__
|
||||
#define __font_h__
|
||||
|
||||
#define FONT_FontFile "assets/fonts/monofur.ttf"
|
||||
|
||||
// Externs
|
||||
extern SDL_Color FONT_FontColor;
|
||||
extern TTF_Font * FONT_FontFamily;
|
||||
// End Externs
|
||||
|
||||
// Prototypes
|
||||
void FONT_Initialize();
|
||||
void FONT_PrintFontStyle(TTF_Font * ffont);
|
||||
void FONT_Deinitialize();
|
||||
void FONT_RenderText(SDL_Renderer * renderer, char * text, SDL_Rect * dstRect);
|
||||
SDL_Texture * FONT_GenerateTexture(SDL_Renderer * renderer, char * text, SDL_Rect * Message_rect);
|
||||
SDL_Surface * FONT_GenerateSurface(char * text, SDL_Rect * Message_rect);
|
||||
void FONT_RenderText(SDL_Renderer * renderer, char * text, SDL_Rect * dstRect, int index);
|
||||
void FONT_RenderTextCentered(SDL_Renderer * renderer, char * text, float scale, int index);
|
||||
SDL_Texture * FONT_GenerateTexture(SDL_Renderer * renderer, char * text, SDL_Rect * Message_rect, int index);
|
||||
SDL_Surface * FONT_GenerateSurface(char * text, SDL_Rect * Message_rect, int index);
|
||||
// End Prototypes
|
||||
|
||||
#endif // __font_h__
|
||||
|
76
highscores.c
76
highscores.c
@ -7,47 +7,25 @@
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
|
||||
#include "highscores.h"
|
||||
#include "font.h"
|
||||
|
||||
#define HIGHSCORES_FontFile "assets/fonts/monofur.ttf"
|
||||
|
||||
int HIGHSCORES_EntriesGot = 0;
|
||||
User * HIGHSCORES_UserList;
|
||||
SDL_Color HIGHSCORES_FontColor;
|
||||
SDL_Texture * HIGHSCORES_TableTexture;
|
||||
SDL_Rect HIGHSCORES_TotalRect;
|
||||
TTF_Font * HIGHSCORES_FontFamily = NULL;
|
||||
SDL_Surface * tempSurface;
|
||||
bool HIGHSCORES_IsInit = false;
|
||||
|
||||
void HIGHSCORES_Initialize(){
|
||||
printf("Initializing Highscores...\n");
|
||||
HIGHSCORES_FontColor = (SDL_Color) {255, 255, 255 };
|
||||
HIGHSCORES_UserList = malloc(10 * sizeof(User));
|
||||
HIGHSCORES_FontFamily = TTF_OpenFont(HIGHSCORES_FontFile, 48);
|
||||
if (!HIGHSCORES_FontFamily) printf("Font could not initialize! Error: %s\n", TTF_GetError());
|
||||
else printf("Font was successfully initialized!\n");
|
||||
printFontStyle(HIGHSCORES_FontFamily);
|
||||
HIGHSCORES_TotalRect = (SDL_Rect) {.x = 0, .y = 0, .w = 1920, .h = 1080 };
|
||||
printf("Highscores initialized!\n");
|
||||
}
|
||||
|
||||
void printFontStyle(TTF_Font * ffont){
|
||||
int style;
|
||||
|
||||
style = TTF_GetFontStyle(ffont);
|
||||
printf("The font style is:");
|
||||
if (style == TTF_STYLE_NORMAL)
|
||||
printf(" normal");
|
||||
else {
|
||||
if (style & TTF_STYLE_BOLD)
|
||||
printf(" bold");
|
||||
if (style & TTF_STYLE_ITALIC)
|
||||
printf(" italic");
|
||||
if (style & TTF_STYLE_UNDERLINE)
|
||||
printf(" underline");
|
||||
if (style & TTF_STYLE_STRIKETHROUGH)
|
||||
printf(" strikethrough");
|
||||
}
|
||||
printf("\n");
|
||||
if (!HIGHSCORES_IsInit) {
|
||||
printf("Initializing Highscores...\n");
|
||||
FONT_Initialize();
|
||||
HIGHSCORES_UserList = malloc(10 * sizeof(User));
|
||||
HIGHSCORES_TotalRect = (SDL_Rect) {.x = 0, .y = 0, .w = 1920, .h = 1080 };
|
||||
printf("Highscores initialized!\n");
|
||||
HIGHSCORES_IsInit = true;
|
||||
} else printf("Highscores already initialized!\n");
|
||||
}
|
||||
|
||||
void HIGHSCORES_Draw(SDL_Renderer * renderer){
|
||||
@ -55,36 +33,35 @@ void HIGHSCORES_Draw(SDL_Renderer * renderer){
|
||||
} /* HIGHSCORES_Draw */
|
||||
|
||||
void HIGHSCORES_Deinitialize(){
|
||||
printf("De-initializing Highscores...\n");
|
||||
TTF_CloseFont(HIGHSCORES_FontFamily);
|
||||
HIGHSCORES_FontFamily = NULL; // to be safe...
|
||||
SDL_DestroyTexture(HIGHSCORES_TableTexture);
|
||||
SDL_FreeSurface(tempSurface);
|
||||
free(HIGHSCORES_UserList);
|
||||
printf("Highscores de-initialized!\n");
|
||||
if (HIGHSCORES_IsInit) {
|
||||
printf("De-initializing Highscores...\n");
|
||||
FONT_Deinitialize();
|
||||
SDL_DestroyTexture(HIGHSCORES_TableTexture);
|
||||
free(HIGHSCORES_UserList);
|
||||
printf("Highscores de-initialized!\n");
|
||||
HIGHSCORES_IsInit = false;
|
||||
} else printf("Highscores already de-initialized!\n");
|
||||
}
|
||||
|
||||
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer){
|
||||
char * buffer = calloc(100, sizeof(char));
|
||||
int count = 0;
|
||||
char format[20] = "| %-58s | %-10s |";
|
||||
char format[25] = "| %-4d | %-52s | %-10s |";
|
||||
SDL_Rect Message_rect;
|
||||
SDL_Surface * HIGHSCORES_TableSurface = SDL_CreateRGBSurface(0, 1920, 1080, 32, 0, 0, 0, 0);
|
||||
|
||||
if (!HIGHSCORES_TableSurface) {
|
||||
printf("Surface wasn't created!\n");
|
||||
}
|
||||
sprintf(buffer, format, "Username", "Score");
|
||||
HIGHSCORES_DrawText(buffer, &Message_rect);
|
||||
Message_rect.y = 70;
|
||||
sprintf(buffer, "| Rank | Username | Score |");
|
||||
SDL_Surface * tempSurface = FONT_GenerateSurface(buffer, &Message_rect, 0);
|
||||
Message_rect.x = 50;
|
||||
Message_rect.h = 50;
|
||||
Message_rect.w = 50;
|
||||
Message_rect.y = 70;
|
||||
SDL_BlitSurface(tempSurface, NULL, HIGHSCORES_TableSurface, &Message_rect);
|
||||
SDL_FreeSurface(tempSurface);
|
||||
while (count < HIGHSCORES_EntriesGot) {
|
||||
sprintf(buffer, format, HIGHSCORES_UserList[count].Username, HIGHSCORES_UserList[count].Score);
|
||||
HIGHSCORES_DrawText(buffer, &Message_rect);
|
||||
sprintf(buffer, format, (count + 1), HIGHSCORES_UserList[count].Username, HIGHSCORES_UserList[count].Score);
|
||||
tempSurface = FONT_GenerateSurface(buffer, &Message_rect, 0);
|
||||
Message_rect.y = ((Message_rect.h + 15) * (count + 1)) + 140;
|
||||
Message_rect.x = 50;
|
||||
SDL_BlitSurface(tempSurface, NULL, HIGHSCORES_TableSurface, &Message_rect);
|
||||
@ -98,11 +75,6 @@ void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer){
|
||||
SDL_FreeSurface(HIGHSCORES_TableSurface);
|
||||
} /* HIGHSCORES_GenerateSurface */
|
||||
|
||||
void HIGHSCORES_DrawText(char * text, SDL_Rect * Message_rect){
|
||||
TTF_SizeText(HIGHSCORES_FontFamily, text, &(Message_rect->w), &(Message_rect->h));
|
||||
tempSurface = TTF_RenderText_Solid(HIGHSCORES_FontFamily, text, HIGHSCORES_FontColor);
|
||||
}
|
||||
|
||||
void HIGHSCORES_ReloadList(){
|
||||
printf("Call BHI interface:\n");
|
||||
system("bhi top output.txt");
|
||||
|
30
main.c
30
main.c
@ -14,6 +14,7 @@
|
||||
#include "highscores.h"
|
||||
#include "settings.h"
|
||||
#include "background.h"
|
||||
#include "font.h"
|
||||
|
||||
#include "main.h"
|
||||
|
||||
@ -105,8 +106,6 @@ void HandleSDLEvents(){
|
||||
running = false;
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
// if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false;
|
||||
// else
|
||||
keyPress(event.key);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
@ -121,28 +120,26 @@ void HandleSDLEvents(){
|
||||
} /* HandleSDLEvents */
|
||||
|
||||
void mousePress(SDL_MouseButtonEvent b){ // Debug prop
|
||||
if (b.button == SDL_BUTTON_LEFT) {
|
||||
printf("Left mouse pressed at %d, %d\n", b.x, b.y);
|
||||
} else if (b.button == SDL_BUTTON_RIGHT) {
|
||||
printf("Right mouse pressed...\n");
|
||||
} else {
|
||||
printf("Unknown mouse button pressed: %d\n", b.button);
|
||||
}
|
||||
// if (b.button == SDL_BUTTON_LEFT) {
|
||||
// printf("Left mouse pressed at %d, %d\n", b.x, b.y);
|
||||
// } else if (b.button == SDL_BUTTON_RIGHT) {
|
||||
// printf("Right mouse pressed...\n");
|
||||
// } else {
|
||||
// printf("Unknown mouse button pressed: %d\n", b.button);
|
||||
// }
|
||||
}
|
||||
|
||||
void keyPress(SDL_KeyboardEvent b){ // Debug prop
|
||||
printf("Key pressed: ID is %d\n", b.keysym.scancode);
|
||||
if (b.keysym.scancode == SDL_SCANCODE_F11 || b.keysym.scancode == SDL_SCANCODE_5) {
|
||||
// printf("Key pressed: ID is %d\n", b.keysym.scancode);
|
||||
if (b.keysym.scancode == SDL_SCANCODE_F11)
|
||||
toggleFullscreen();
|
||||
}
|
||||
}
|
||||
|
||||
void toggleFullscreen(){
|
||||
if (fullscreen) {
|
||||
if (fullscreen)
|
||||
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
||||
} else {
|
||||
else
|
||||
SDL_SetWindowFullscreen(window, 0);
|
||||
}
|
||||
fullscreen = !fullscreen;
|
||||
}
|
||||
|
||||
@ -150,7 +147,6 @@ void windowChanged(SDL_WindowEvent b){ // Debug prop
|
||||
switch (b.event) {
|
||||
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
||||
printf("Window was resized to (%d|%d)!\n", event.window.data1, event.window.data2);
|
||||
// BREAKOUT_ChangeSize(event.window.data1, event.window.data2);
|
||||
XScale = ((double)(event.window.data1) / (double)width);
|
||||
YScale = ((double)(event.window.data2) / (double)height);
|
||||
SDL_RenderSetScale(renderer, XScale, YScale);
|
||||
@ -182,6 +178,7 @@ void INITIALIZE() {
|
||||
BREAKOUT_INITIALIZE(renderer, width, height);
|
||||
scenery = BREAKOUT_CreateDefault();
|
||||
Load_Textures(renderer);
|
||||
FONT_Initialize();
|
||||
HIGHSCORES_Initialize();
|
||||
BACKGROUND_Initialize(renderer, width, height);
|
||||
Settings_Initialize(renderer);
|
||||
@ -193,6 +190,7 @@ void QUIT(){
|
||||
BACKGROUND_Deinitialize();
|
||||
Settings_Deinitialize();
|
||||
HIGHSCORES_Deinitialize();
|
||||
FONT_Deinitialize();
|
||||
BREAKOUT_DestroyObject(&scenery);
|
||||
BREAKOUT_DEINITIALIZE();
|
||||
TTF_Quit();
|
||||
|
Loading…
Reference in New Issue
Block a user