1 Commits
dev ... Alpha

Author SHA1 Message Date
bf6b884523 Changed font lib, game start countdown added 2018-01-21 21:10:27 +01:00
13 changed files with 194 additions and 267 deletions

1
.gitignore vendored
View File

@ -8,7 +8,6 @@ sdl2-config
*.o
*.psd
*.exe
*.json
!bhi.exe
.tags*
*.txt

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 MiB

After

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 184 KiB

View File

@ -9,33 +9,24 @@
#include "breakout.h"
#include "vector.h"
#include "font.h"
extern float XScale, YScale;
#define BALL_TexturePath "assets/images/ball.png"
#define PADDLE_TexturePath "assets/images/paddle.png"
#define BLOCK_TexturePath "assets/images/spritesheet.png"
#define BRAEKOUT_CountdownTexturePath "assets/images/text.png"
#define BALL_MinSpeed 8.0f
#define BALL_MaxSpeed 50.0f
#define BALL_AccelerationTime 18000
#ifndef __nullptr__
#define Nullptr(type) (type *)0
#endif // __nullptr__
float PADDLE_SmoothFactor = 0.1f;
int BLOCK_TextureCount = 24;
int BALL_TextureCount = 9;
int GAME_CountdownTextureCount = 4;
int PADDLE_TextureCount = 9;
int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight;
SDL_Texture * BALL_Texture;
SDL_Texture * GAME_CountdownTexture;
SDL_Texture * PADDLE_Texture;
SDL_Texture * BLOCK_Texture;
SDL_Rect * BALL_SourceRects;
SDL_Rect * GAME_CountdownSourceRects;
SDL_Rect * PADDLE_SourceRects;
SDL_Rect * BLOCK_SourceRects;
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
@ -53,15 +44,6 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
BALL_Initialize(renderer);
PADDLE_Initialize(renderer);
BLOCK_Initialize(renderer);
GAME_CountdownTexture = IMG_LoadTexture(renderer, BRAEKOUT_CountdownTexturePath);
if (!GAME_CountdownTexture) printf("Coutndown texture failed to load!\n");
GAME_CountdownTextureCount = 4;
GAME_CountdownSourceRects = (SDL_Rect *)malloc(GAME_CountdownTextureCount * sizeof(SDL_Rect));
if (!GAME_CountdownSourceRects) printf("FATAL! Memory allocation failed!\n");
GAME_CountdownSourceRects[0] = (SDL_Rect) {.x = 1, .y = 668, .w = 1000, .h = 732 };
GAME_CountdownSourceRects[1] = (SDL_Rect) {.x = 1, .y = 1, .w = 242, .h = 665 };
GAME_CountdownSourceRects[2] = (SDL_Rect) {.x = 245, .y = 1, .w = 443, .h = 665 };
GAME_CountdownSourceRects[3] = (SDL_Rect) {.x = 690, .y = 1, .w = 443, .h = 665 };
printf("Game initialized!\n");
BREAKOUT_IsInit = true;
} else printf("Game is already initialized!\n");
@ -76,12 +58,9 @@ Scenery BREAKOUT_CreateDefault(){
scenery.ball = BALL_CreateDefault();
scenery.paddle = PADDLE_CreateDefault();
scenery.blocks = malloc(scenery.BlockCount * sizeof(Block));
scenery.Frames = 0;
scenery.Score = 0;
if (!(scenery.blocks)) printf("FATAL! Memory allocation failed!\n");
scenery.IsPaused = false;
scenery.Lives = 3;
scenery.DestroyedBlocks = 0;
int index;
for (int y = 0; y < 9; y++) {
index = 15 * y;
@ -94,13 +73,6 @@ Scenery BREAKOUT_CreateDefault(){
return scenery;
} /* BREAKOUT_CreateDefault */
int BREAKOUT_RefreshScore(Scenery * scenery){
(scenery->Score) = (int)round((double)(scenery->Frames) * 0.005f * ((scenery->ball).Speed) + (double)(50 * (scenery->DestroyedBlocks)));
printf("Score: %d\n", (scenery->Score));
return (scenery->Score);
}
// This Function is obsolete! Do not use it!
void BREAKOUT_ChangeSize(int width, int height){
BREAKOUT_BoxWidth = width;
@ -108,22 +80,15 @@ 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;
}
(scenery->Frames)++;
BREAKOUT_RefreshScore(scenery);
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));
scenery->StartCountdown = 240;
scenery->IsGameOver = false;
scenery->Frames = 0;
if (--(scenery->Lives) <= 0)
printf("Game over, no lives left!\n");
else
@ -143,25 +108,29 @@ void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer){
}
BALL_Draw(renderer, &(scenery->ball));
PADDLE_Draw(renderer, &(scenery->paddle));
if ((scenery->StartCountdown) > 0) { // ! Render Z-Layer !
SDL_Rect * rect = GAME_CountdownSourceRects + (((scenery->StartCountdown) - 1) / 60);
SDL_Rect target = *rect;
target.x = ((BREAKOUT_BoxWidth - (rect->w)) / 2);
target.y = ((BREAKOUT_BoxHeight - (rect->h)) / 2);
SDL_RenderCopy(renderer, GAME_CountdownTexture, rect, &target);
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(){
if (BREAKOUT_IsInit) {
printf("De-initializing Game...\n");
SDL_DestroyTexture(GAME_CountdownTexture);
free(PADDLE_MoveLeftKeys);
free(PADDLE_MoveRightKeys);
free(BALL_SourceRects);
free(PADDLE_SourceRects);
free(BLOCK_SourceRects);
free(GAME_CountdownSourceRects);
BALL_Deinitialize();
PADDLE_Deinitialize();
BLOCK_Deinitialize();
@ -184,33 +153,24 @@ void BALL_Initialize(SDL_Renderer * renderer){
printf("Initializing Ball...\n");
BALL_Texture = IMG_LoadTexture(renderer, BALL_TexturePath);
if (!BALL_Texture) printf("Ball texture failed to load!\n");
BALL_TextureCount = 9;
BALL_SourceRects = (SDL_Rect *)malloc(BALL_TextureCount * sizeof(SDL_Rect));
BALL_SourceRects = (SDL_Rect *)malloc(1 * sizeof(SDL_Rect));
if (!BALL_SourceRects) printf("FATAL! Memory allocation failed!\n");
BALL_SourceRects[0] = (SDL_Rect) {.x = 1029, .y = 1029, .w = 512, .h = 512 };
BALL_SourceRects[1] = (SDL_Rect) {.x = 1, .y = 1, .w = 512, .h = 512 };
BALL_SourceRects[2] = (SDL_Rect) {.x = 1, .y = 515, .w = 512, .h = 512 };
BALL_SourceRects[3] = (SDL_Rect) {.x = 1, .y = 1029, .w = 512, .h = 512 };
BALL_SourceRects[4] = (SDL_Rect) {.x = 515, .y = 1, .w = 512, .h = 512 };
BALL_SourceRects[5] = (SDL_Rect) {.x = 1029, .y = 1, .w = 512, .h = 512 };
BALL_SourceRects[6] = (SDL_Rect) {.x = 515, .y = 515, .w = 512, .h = 512 };
BALL_SourceRects[7] = (SDL_Rect) {.x = 515, .y = 1029, .w = 512, .h = 512 };
BALL_SourceRects[8] = (SDL_Rect) {.x = 1029, .y = 515, .w = 512, .h = 512 };
BALL_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 512, .h = 512 };
printf("Ball initialized!\n");
BALL_IsInit = true;
} else printf("Ball is already initialized!\n");
} /* BALL_Initialize */
}
Ball BALL_CreateDefault(){
double rotation = (double)(rand() % 360);
return (Ball) {
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2 - 15, .y = BREAKOUT_BoxHeight - 132 },
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2 - 15, .y = BREAKOUT_BoxHeight - 130 },
.Momentum = (Vector) {.x = 0.0f, .y = 15.0f },
.TargetRect = (SDL_Rect) {.x = BREAKOUT_BoxWidth / 2 - 15, .y = BREAKOUT_BoxHeight - 130, .w = 30, .h = 30 },
.Size = 15.0f,
.Rotation = rotation,
.RotationValue = 5,
.RotationValue = 2,
.TextureIndex = 0,
.Speed = 15.0f
}; // Objekt für die Eigenschaften des Balls
@ -220,7 +180,6 @@ void BALL_ResetPosition(Ball * obj){
(obj->Location).x = BREAKOUT_BoxWidth / 2 - 15;
(obj->Location).y = BREAKOUT_BoxHeight - 130;
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
(obj->Momentum) = VECTOR_GetScaledDirectionalUnitVector(0.0f, (obj->Speed));
}
void BALL_Draw(SDL_Renderer * renderer, Ball * obj){
@ -340,16 +299,7 @@ bool BALL_CollideWithPaddle(Ball * obj, Paddle * paddle){
return false;
} /* BALL_CollideWithPaddle */
void BALL_AdaptSpeedGradient(Ball * obj, int FrameCount){
if (FrameCount > BALL_AccelerationTime)
obj->Speed = BALL_MaxSpeed;
else
obj->Speed = BALL_MinSpeed + (((double)FrameCount / (double)BALL_AccelerationTime) * (BALL_MaxSpeed - BALL_MinSpeed));
}
void BALL_Update(Ball * obj, Scenery * scenery){
BALL_AdaptSpeedGradient(obj, (scenery->Frames));
(obj->Momentum) = VECTOR_ChangeScaleTo((obj->Momentum), (obj->Speed));
Block * blocks = (scenery->blocks);
Paddle * paddle = &(scenery->paddle);
int BlockCount = scenery->BlockCount;
@ -367,8 +317,6 @@ void BALL_Update(Ball * obj, Scenery * scenery){
oldLocation = obj->Location;
if (BALL_CollideWithRect(obj, &(blocks[i].TargetRect))) {
BLOCK_DealDamage(blocks + i, 1);
if (blocks[i].HP <= 0)
(scenery->DestroyedBlocks)++;
(obj->Location) = VECTOR_Add(oldLocation, (obj->Momentum));
BALL_MoveAwayFromBoundaries(obj);
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
@ -423,7 +371,7 @@ Paddle PADDLE_CreateDefault(){
.TextureIndex = 0,
.Speed = 10,
.SteeringAngle = 40.0f,
.Mode = MouseControl
.Mode = KeyboardControl
}; // Objekt für die Eigenschaften des Balls
}
@ -435,6 +383,8 @@ void PADDLE_ResetPosition(Paddle * obj){
void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj){
// printf("Paddle drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x);
SDL_RenderCopy(renderer, PADDLE_Texture, PADDLE_SourceRects + (obj->TextureIndex), &(obj->TargetRect));
// SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
// SDL_RenderDrawRect(renderer, &(obj->TargetRect));
}
bool KeyPressed(const Uint8 * keystate, Uint8 * keyArray){
@ -458,36 +408,36 @@ void DOUBLE_Constrain(double * variable, double min, double max){
*variable = min;
}
void PADDLE_MoveSmooth(Paddle * obj){
int mouseX, paddleXMid, halfPaddle = ((obj->TargetRect).w / 2);
SDL_GetMouseState(&mouseX, NULL);
mouseX = (int)roundf((float)mouseX / XScale);
paddleXMid = halfPaddle + (obj->TargetRect.x); // Current State
(obj->TargetRect).x = paddleXMid - (int)roundf((float)(paddleXMid - mouseX) * PADDLE_SmoothFactor) - halfPaddle;
}
void PADDLE_Update(Paddle * obj, const Uint8 * keystate){
bool leftKeyPressed, rightKeyPressed;
bool leftKeyPressed = false, rightKeyPressed = false;
int paddleXMid = (obj->TargetRect).x + ((obj->TargetRect).w / 2);
int mouseX;
switch (obj->Mode) {
case MouseControl:
PADDLE_MoveSmooth(obj);
SDL_GetMouseState(&mouseX, NULL);
mouseX = (int)roundf((float)mouseX / XScale);
if (abs(mouseX - paddleXMid) > (obj->Speed)) {
if (mouseX > paddleXMid)
rightKeyPressed = true;
else
leftKeyPressed = true;
}
break;
case KeyboardControl:
leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys);
rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys);
break;
default:
printf("Unknown Paddle Control Mode: %d!\n", obj->Mode);
break;
}
if (leftKeyPressed && (!rightKeyPressed)) {
((obj->TargetRect).x) -= (obj->Speed);
} else if ((!leftKeyPressed) && rightKeyPressed) {
((obj->TargetRect).x) += (obj->Speed);
}
break;
default:
printf("Unknown Paddle Control Mode: %d!\n", obj->Mode);
break;
} /* switch */
INT_Constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
} /* PADDLE_Update */

View File

@ -39,7 +39,7 @@ typedef struct sceneryStruct {
Ball ball;
Paddle paddle;
Block * blocks;
int BlockCount, Lives, StartCountdown, Frames, Score, DestroyedBlocks;
int BlockCount, Lives, StartCountdown;
bool IsPaused, IsGameOver;
} Scenery; // Objekt für die Objekte und Eigenschaften einer Szenerie
// End Structs
@ -47,7 +47,6 @@ typedef struct sceneryStruct {
// Prototypes
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height);
Scenery BREAKOUT_CreateDefault();
int BREAKOUT_RefreshScore(Scenery * scenery);
void BREAKOUT_ChangeSize(int width, int height);
void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate);
void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer);
@ -65,7 +64,6 @@ SDL_Point BALL_GetCenter(Ball * obj);
void BALL_CollideWithBorders(Ball * obj);
void BALL_MoveAwayFromBoundaries(Ball * obj);
bool BALL_CollideWithPaddle(Ball * obj, Paddle * paddle);
void BALL_AdaptSpeedGradient(Ball * obj, int FrameCount);
void BALL_Update(Ball * obj, Scenery * scenery);
void BALL_DestroyObject(Ball * obj);
void BALL_Deinitialize();
@ -76,7 +74,6 @@ void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj);
bool KeyPressed(const Uint8 * keystate, Uint8 * keyArray);
void INT_Constrain(int * variable, int min, int max);
void DOUBLE_Constrain(double * variable, double min, double max);
void PADDLE_MoveSmooth(Paddle * obj);
void PADDLE_Update(Paddle * obj, const Uint8 * keystate);
void PADDLE_DestroyObject(Paddle * obj);
void PADDLE_Deinitialize();

78
font.c
View File

@ -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(){
if (!FONT_IsInit) {
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);
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(){
if (FONT_IsInit) {
printf("De-initializing Font...\n");
TTF_CloseFont(FONT_FontFamily);
FONT_FontFamily = NULL; // to be safe...
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
View File

@ -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__

View File

@ -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(){
if (!HIGHSCORES_IsInit) {
printf("Initializing Highscores...\n");
HIGHSCORES_FontColor = (SDL_Color) {255, 255, 255 };
FONT_Initialize();
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");
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(){
if (HIGHSCORES_IsInit) {
printf("De-initializing Highscores...\n");
TTF_CloseFont(HIGHSCORES_FontFamily);
HIGHSCORES_FontFamily = NULL; // to be safe...
FONT_Deinitialize();
SDL_DestroyTexture(HIGHSCORES_TableTexture);
SDL_FreeSurface(tempSurface);
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] = "| %-54s | %-14s |";
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");

34
main.c
View File

@ -14,6 +14,7 @@
#include "highscores.h"
#include "settings.h"
#include "background.h"
#include "font.h"
#include "main.h"
@ -55,7 +56,7 @@ int main(int argc, char * args[]){
HIGHSCORES_Draw(renderer);
break;
case Settings:
Settings_Draw(renderer,&scenery);
Settings_Draw(renderer);
break;
default:
printf("Unknow state was updated: %d\n", gameState);
@ -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,9 +178,10 @@ 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,&scenery);
Settings_Initialize(renderer);
printf("Initializing finished!\n");
} /* INITIALIZE */
@ -193,6 +190,7 @@ void QUIT(){
BACKGROUND_Deinitialize();
Settings_Deinitialize();
HIGHSCORES_Deinitialize();
FONT_Deinitialize();
BREAKOUT_DestroyObject(&scenery);
BREAKOUT_DEINITIALIZE();
TTF_Quit();

View File

@ -9,47 +9,37 @@
#define Slider_height 100
#define Scalar_width 20
#define Bar_width 400
#define round(x) ((int) ((x) + .5))
#define distance(x1,y1,x2,y2) ((int)(sqrt(pow(x2-x1,2)+pow(y2-y1,2))))
SDL_Texture* Settings_Texture;
SDL_Texture* Settings_Return_Button_Texture;
SDL_Texture* Settings_Ball_Texture;
SDL_Rect Settings_rect;
SDL_Rect Settings_Return_Button_rect;
SDL_Rect Settings_Ball_rect;
Slider BV;
Slider BS;
Slider BT;
Uint32 Mousestate;
bool Settings_IsInit=false;
void Settings_Initialize (SDL_Renderer* renderer,Scenery* scenery) {
Initialize_Slider(400,300,Scalar_width,Bar_width,Slider_height,1,2,&BV,1.5);
Initialize_Slider(400,500,Scalar_width,Bar_width,Slider_height,30,100,&BS,scenery->ball.TargetRect.w);
Initialize_Slider(400,700,Scalar_width,Bar_width,Slider_height,0,8,&BT,scenery->ball.TextureIndex);
void Settings_Initialize (SDL_Renderer* renderer) {
Initialize_Slider(400,300,Scalar_width,Bar_width,Slider_height,1,2,&BV);
Initialize_Slider(400,500,Scalar_width,Bar_width,Slider_height,1,2,&BS);
Initialize_Slider(400,700,Scalar_width,Bar_width,Slider_height,1,2,&BT);
Settings_Texture = IMG_LoadTexture(renderer, "assets/images/settings_title.png");
Settings_rect = (SDL_Rect){.x = 800, .y = 180, .w=313, .h=100};
Settings_Return_Button_Texture = IMG_LoadTexture(renderer, "assets/images/return_button.png");
Settings_Return_Button_rect = (SDL_Rect){.x = 200, .y = 200, .w=75, .h=75};
Settings_Ball_Texture = IMG_LoadTexture(renderer, "assets/images/ball.png");
Settings_Ball_rect = (SDL_Rect){.x = 1200, .y = 300, .w=100, .h=100};
Settings_IsInit = true;
}
void Settings_Draw (SDL_Renderer* renderer,Scenery* scenery) {
void Settings_Draw (SDL_Renderer* renderer) {
double x;
scenery->ball.TargetRect.x=900;
scenery->ball.TargetRect.y=700;
SDL_RenderCopy(renderer, Settings_Texture, NULL, &Settings_rect);
SDL_RenderCopy(renderer, Settings_Return_Button_Texture, NULL, &Settings_Return_Button_rect);
SDL_RenderCopy(renderer, Settings_Ball_Texture, NULL, &Settings_Ball_rect);
Draw_Slider(renderer,&BV);
Draw_Slider(renderer,&BS);
Draw_Slider(renderer,&BT);
Draw_Ballstate(renderer,scenery);
mapping(&x,&BT);
scenery->ball.TextureIndex=round(x);
mapping(&x,&BS);
scenery->ball.TargetRect.w=x;
scenery->ball.TargetRect.h=x;
Settings_Return();
}
void Settings_Deinitialize(){
@ -64,20 +54,20 @@ void Draw_Slider(SDL_Renderer* renderer,Slider* beta){
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_RenderDrawRect(renderer,&beta->Bar_rect);
int x,y;
Mousestate=SDL_GetMouseState(&x,&y);
if(y<=((beta->Bar_rect.y)+(beta->Bar_rect.h))&&y>=(beta->Bar_rect.y)&&x<=(beta->Bar_rect.w+beta->Bar_rect.x)&&x>=(beta->Bar_rect.x)&&(Mousestate & SDL_BUTTON(SDL_BUTTON_LEFT))){
Uint32 Mousestate=SDL_GetMouseState(&x,&y);
if(y<=((beta->y)+(beta->h))&&y>=(beta->y)&&x<=(beta->bw+beta->x)&&x>=(beta->x)&&(Mousestate & SDL_BUTTON(SDL_BUTTON_LEFT))){
SDL_RenderFillRect(renderer,&beta->Scalar_rect);
SDL_RenderDrawRect(renderer,&beta->Scalar_rect);
if(x>(beta->Bar_rect.x+beta->Bar_rect.w-(beta->Scalar_rect.w)/2)){
beta->Scalar_rect.x=(beta->Bar_rect.x+beta->Bar_rect.w-(beta->Scalar_rect.w));
beta->Slider_value=(beta->Bar_rect.x+beta->Bar_rect.w-(beta->Scalar_rect.w)/2);
if(x>(beta->x+beta->bw-(beta->sw)/2)){
beta->Scalar_rect.x=(beta->x+beta->bw-(beta->sw));
beta->Slider_value=(beta->x+beta->bw-(beta->sw)/2);
}
else if(x<beta->Bar_rect.x+(beta->Scalar_rect.w)/2){
beta->Scalar_rect.x=beta->Bar_rect.x;
beta->Slider_value=beta->Bar_rect.x+(beta->Scalar_rect.w)/2;
else if(x<beta->x+(beta->sw)/2){
beta->Scalar_rect.x=beta->x;
beta->Slider_value=beta->x+(beta->sw)/2;
}
else{
beta->Scalar_rect.x=x-(beta->Scalar_rect.w/2);
beta->Scalar_rect.x=x-(beta->sw/2);
beta->Slider_value=x;
}
}
@ -86,25 +76,19 @@ void Draw_Slider(SDL_Renderer* renderer,Slider* beta){
}
}
void Draw_Ballstate(SDL_Renderer* renderer,Scenery* scenery){
BALL_Draw(renderer, &(scenery->ball));
}
void mapping(double *x,Slider* beta){
*x=((beta->max-beta->min)/(beta->Bar_rect.w-(beta->Scalar_rect.w)))*(beta->Slider_value-beta->Bar_rect.x-beta->Scalar_rect.w/2)+beta->min;
*x=((beta->max-beta->min)/(beta->bw-(beta->sw)))*(beta->Slider_value-beta->x-beta->sw/2)+beta->min;
}
void Initialize_Slider(int x,int y,int sw,int bw,int h,double min,double max,Slider* beta,double defaultvalue){
beta->Scalar_rect = (SDL_Rect){.x=(defaultvalue-min)/(max-min)*(bw-sw/2)+x,.y=y,.w=sw,.h=h};
void Initialize_Slider(int x,int y,int sw,int bw,int h,double min,double max,Slider* beta){
beta->Scalar_rect = (SDL_Rect){.x=x,.y=y,.w=sw,.h=h};
beta->Bar_rect = (SDL_Rect){.x=x,.y=y,.w=bw,.h=h};
beta->x=x;
beta->y=y;
beta->sw=sw;
beta->bw=bw;
beta->h=h;
beta->max=max;
beta->min=min;
beta->Slider_value=(defaultvalue-min)/(max-min)*(bw-sw/2)+x;
}
void Settings_Return(Scenery* scenery){
int x,y;
Mousestate=SDL_GetMouseState(&x,&y);
if((distance(x,y,237,237)<=37)&&(Mousestate & SDL_BUTTON(SDL_BUTTON_LEFT)))
GAME_ChangeState(MainMenu);
beta->Slider_value=x+sw/2;
}

View File

@ -7,32 +7,24 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>
#include <math.h>
#include "breakout.h"
#include "gamestate.h"
#include "main.h"
typedef struct sliderstruct {
SDL_Rect Bar_rect;
SDL_Rect Scalar_rect;
int x,y,bw,sw,h;
double Slider_value,min,max;
} Slider;
void Settings_Initialize (SDL_Renderer* renderer);
void Settings_Draw (SDL_Renderer* renderer,Scenery* scenery);
void Draw_Slider(SDL_Renderer* renderer,Slider* beta);
void Draw_Ballstate(SDL_Renderer* renderer,Scenery* scenery);
void Settings_Initialize (SDL_Renderer* renderer,Scenery* scenery);
void Initialize_Slider(int x,int y,int sw,int bw,int h,double min,double max,Slider* beta,double defaultvalue);
void Settings_Draw (SDL_Renderer* renderer);
void Settings_Deinitialize();
void Draw_Slider(SDL_Renderer* renderer,Slider* beta);
void mapping(double *x,Slider* beta);
void Settings_Return();
void Initialize_Slider(int x,int y,int sw,int bw,int h,double min,double max,Slider* beta);
#endif

View File

@ -5,7 +5,6 @@
#include "gamestate.h"
#include "main.h"
extern float XScale, YScale;
SDL_Texture * TITLE_Texture;
SDL_Texture * PLAYBUTTON_Texture;
@ -24,11 +23,7 @@ SDL_Rect HIGHSCORESBUTTON_Rect;
SDL_Rect QUITBUTTON_Rect;
int clickInRect(SDL_MouseButtonEvent b, SDL_Rect * area_rect) {
int clickx, clicky;
clickx = (int)roundf((float)b.x / XScale);
clicky = (int)roundf((float)b.y / YScale);
return ((clickx >= (area_rect->x)) && (clickx <= ((area_rect->x) + (area_rect->w))) && (clicky >= (area_rect->y)) && (clicky <= ((area_rect->y) + (area_rect->h))));
return (((b.x) >= (area_rect->x)) && ((b.x) <= ((area_rect->x) + (area_rect->w))) && ((b.y) >= (area_rect->y)) && ((b.y) <= ((area_rect->y) + (area_rect->h))));
}
void Load_Textures(SDL_Renderer * renderer) {