Highscoretable works now (mostly)

This commit is contained in:
Michael Chen 2018-01-15 23:47:39 +01:00
parent 4ede84786e
commit 5db58e9d38
5 changed files with 108 additions and 56 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,11 +1,93 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "highscores.h"
User * HIGHSCORES_GetList(int * usercount){
int entriesGot = 0;
User * ul;
SDL_Color White;
SDL_Texture * Message;
SDL_Surface * surfaceMessage;
SDL_Rect Message_rect;
TTF_Font * font = NULL;
void HIGHSCORES_Initialize(){
printf("Initializing Highscores...\n");
White = (SDL_Color) {255, 255, 255 };
ul = malloc(10 * sizeof(User));
font = TTF_OpenFont("assets/fonts/monofur.ttf", 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");
}
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");
}
void HIGHSCORES_Draw(SDL_Renderer * renderer){
char * buffer = calloc(100, sizeof(char));
int count = 0;
char format[20] = "| %-50s | %-10s |";
sprintf(buffer, format, "Username", "Score");
HIGHSCORES_GenerateTexture(renderer, buffer);
Message_rect.y = 70;
Message_rect.x = 50;
SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
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.x = 50;
SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
count++;
}
}
void HIGHSCORES_Deinitialize(){
printf("De-initializing Highscores...\n");
TTF_CloseFont(font);
font = NULL; // to be safe...
SDL_FreeSurface(surfaceMessage);
SDL_DestroyTexture(Message);
free(ul);
printf("Highscores de-initialized!\n");
}
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer, char * text){
int w, h;
TTF_SizeText(font, text, &w, &h);
surfaceMessage = TTF_RenderText_Solid(font, text, White); // as TTF_RenderText_Solid could only be used on SDL_Surface then you have to create the surface first
Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); // now you can convert it into a texture
Message_rect = (SDL_Rect) {.x = 0, .y = 0, .w = w, .h = h }; // create a rect
}
void HIGHSCORES_ReloadList(int * usercount){
printf("Call BHI interface:\n");
system("bhi top output.txt");
printf("BHI interface quit!\nBHI output handling...\n");
@ -18,13 +100,13 @@ User * HIGHSCORES_GetList(int * usercount){
char * name, * scorestring;
int nameCharCount = 0, scoreCharCount = 0;
bool switchread = false;
User * ul = malloc(10 * sizeof(User));
ul = malloc(10 * sizeof(User));
fp = fopen("output.txt", "r");
if (fp == NULL)
return ul;
return;
if ((read = getline(&line, &len, fp)) != -1)
if (line[0] == 0)
return ul;
return;
int counter = 0;
while ((read = getline(&line, &len, fp)) != -1) {
name = malloc(read * sizeof(char));
@ -50,10 +132,12 @@ User * HIGHSCORES_GetList(int * usercount){
name[nameCharCount] = '\0';
scorestring[scoreCharCount] = '\0';
User tmp;
tmp.Username = name;
tmp.Score = scorestring;
strcpy(tmp.Username, name);
strcpy(tmp.Score, scorestring);
ul[counter++] = tmp;
}
free(name);
free(scorestring);
fclose(fp);
if (line)
@ -64,5 +148,4 @@ User * HIGHSCORES_GetList(int * usercount){
*usercount = counter;
printf("BHI Interface successfully quit!\n");
return ul;
} /* main */

View File

@ -3,8 +3,17 @@
// Structs
typedef struct userStruct {
char * Username, * Score;
char Username[50], Score[30];
} User;
// End Structs
// Prototypes
void HIGHSCORES_Initialize();
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);
// End Prototypes
#endif

54
main.c
View File

@ -11,6 +11,7 @@
#include "vector.h"
#include "startmenu.h"
#include "gamestate.h"
#include "highscores.h"
#ifndef __nullptr__
#define Nullptr(type) (type *)0
@ -24,7 +25,6 @@
void INITIALIZE();
void QUIT();
void HandleSDLEvents();
void DrawText(SDL_Renderer * renderer, const char * text);
void DrawBackground(SDL_Renderer * renderer);
void printFontStyle(TTF_Font * ffont);
void mousePress(SDL_MouseButtonEvent b);
@ -44,7 +44,6 @@ SDL_Window * window;
SDL_Renderer * renderer;
SDL_Event event;
bool running = true, fullscreen = false;
TTF_Font * font = NULL;
GameState gameState = Game;
Scenery scenery;
@ -64,6 +63,9 @@ int main(int argc, char * args[]){
// Startmenu_Update(keystate);
Startmenu_Draw(renderer);
break;
case Highscores:
HIGHSCORES_Draw(renderer);
break;
default:
printf("Unknow state was updated: %d\n", gameState);
}
@ -78,6 +80,7 @@ void HandleSDLEvents(){
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
printf("NOTICE: User manually quit window!\n");
running = false;
break;
case SDL_KEYDOWN:
@ -137,43 +140,6 @@ void DrawBackground(SDL_Renderer * renderer){
SDL_RenderClear(renderer);
} /* DrawFrame */
void DrawText(SDL_Renderer * renderer, const char * text){
int w, h;
if (TTF_SizeText(font, text, &w, &h)) {
// perhaps print the current TTF_GetError(), the string can't be rendered...
} else {
printf("width=%d height=%d\n", w, h);
}
SDL_Color White = { 255, 255, 255 }; // this is the color in rgb format, maxing out all would give you the color white, and it will be your text's color
SDL_Surface * surfaceMessage = TTF_RenderText_Solid(font, text, White); // as TTF_RenderText_Solid could only be used on SDL_Surface then you have to create the surface first
SDL_Texture * Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); // now you can convert it into a texture
SDL_Rect Message_rect = (SDL_Rect) {.x = 0, .y = 0, .w = w, .h = h }; // create a rect
SDL_RenderCopy(renderer, Message, NULL, &Message_rect); // you put the renderer's name first, the Message, the crop size(you can ignore this if you don't want to dabble with cropping), and the rect which is the size and coordinate of your texture
SDL_FreeSurface(surfaceMessage);
SDL_DestroyTexture(Message);
}
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");
}
void INITIALIZE() {
printf("Initializing started...\n");
srand(time(NULL));
@ -185,12 +151,6 @@ void INITIALIZE() {
if (TTF_Init() == -1) printf("TTF could not initialize! Error: %s\n", TTF_GetError());
else printf("TTF was successfully initialized!\n");
// load font.ttf at size 16 into font
font = TTF_OpenFont("assets/fonts/minecraft.ttf", 32);
if (!font) printf("Font could not initialize! Error: %s\n", TTF_GetError());
else printf("Font was successfully initialized!\n");
printFontStyle(font);
window = SDL_CreateWindow("BreakING", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
SDL_SetWindowResizable(window, true);
printf("Window was created!\n");
@ -199,15 +159,15 @@ void INITIALIZE() {
BREAKOUT_INITIALIZE(renderer, width, height);
scenery = BREAKOUT_CreateDefault();
Load_Textures(renderer);
HIGHSCORES_Initialize();
printf("Initializing finished!\n");
} /* INITIALIZE */
void QUIT(){
printf("De-initializing started...\n");
HIGHSCORES_Deinitialize();
BREAKOUT_DestroyObject(&scenery);
BREAKOUT_DEINITIALIZE();
TTF_CloseFont(font);
font = NULL; // to be safe...
TTF_Quit();
IMG_Quit();
printf("Quitting SDL_IMG finished!\n");