207 lines
7.0 KiB
C
207 lines
7.0 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <math.h>
|
|
#include <time.h>
|
|
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_image.h>
|
|
#include <SDL2/SDL_ttf.h>
|
|
|
|
#include "breakout.h"
|
|
#include "vector.h"
|
|
#include "startmenu.h"
|
|
|
|
#ifndef __nullptr__
|
|
#define Nullptr(type) (type *)0
|
|
#endif // __nullptr__
|
|
|
|
typedef enum gameStateEnum { MainMenu, Game, LevelSelect, Settings, Highscores } GameState;
|
|
|
|
void INITIALIZE();
|
|
void QUIT();
|
|
void DrawText(SDL_Renderer * renderer, const char * text);
|
|
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();
|
|
|
|
const int width = 1920;
|
|
const int height = 1080;
|
|
|
|
float XScale = 1.0f, YScale = 1.0f;
|
|
|
|
Uint8 * keystate; // TODO: export all this into scenery and enemy waves
|
|
SDL_Window * window;
|
|
SDL_Renderer * renderer;
|
|
SDL_Event event;
|
|
bool running = true, fullscreen = false;
|
|
TTF_Font * font = NULL;
|
|
GameState gameState = MainMenu;
|
|
|
|
Ball ball;
|
|
|
|
int main(int argc, char * args[]){
|
|
// system("bhi.exe");
|
|
INITIALIZE();
|
|
while (running) { // Gameloop
|
|
while (SDL_PollEvent(&event)) {
|
|
switch (event.type) {
|
|
case SDL_QUIT:
|
|
running = false;
|
|
break;
|
|
case SDL_KEYDOWN:
|
|
if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false;
|
|
else keyPress(event.key);
|
|
break;
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
mousePress(event.button);
|
|
break;
|
|
case SDL_WINDOWEVENT:
|
|
windowChanged(event.window);
|
|
break;
|
|
}
|
|
}
|
|
keystate = SDL_GetKeyboardState(NULL);
|
|
DrawBackground(renderer);
|
|
switch (gameState) {
|
|
case Game:
|
|
BREAKOUT_Update(keystate);
|
|
BREAKOUT_Draw(renderer);
|
|
break;
|
|
case MainMenu:
|
|
Startmenu_Draw(renderer);
|
|
break;
|
|
default:
|
|
printf("Unknow state was updated: %d\n", gameState);
|
|
}
|
|
DrawText(renderer, "FICK DICH");
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
QUIT();
|
|
return 0;
|
|
} /* main */
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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) {
|
|
toggleFullscreen();
|
|
}
|
|
}
|
|
|
|
void toggleFullscreen(){
|
|
if (fullscreen) {
|
|
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
|
} else {
|
|
SDL_SetWindowFullscreen(window, 0);
|
|
}
|
|
fullscreen = !fullscreen;
|
|
}
|
|
|
|
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);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void DrawBackground(SDL_Renderer * renderer){
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
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() {
|
|
srand(time(NULL));
|
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) printf("SDL could not initialize! Error: %s\n", SDL_GetError());
|
|
else printf("SDL was successfully initialized!\n");
|
|
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) printf("IMG could not initialize! Error: %s\n", IMG_GetError());
|
|
else printf("IMG was successfully initialized!\n");
|
|
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");
|
|
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
printf("Renderer was created!\n");
|
|
BREAKOUT_INITIALIZE(renderer, width, height);
|
|
Load_Textures(renderer);
|
|
} /* INITIALIZE */
|
|
|
|
void QUIT(){
|
|
printf("De-initializing started...\n");
|
|
BREAKOUT_DEINITIALIZE();
|
|
free(keystate);
|
|
TTF_CloseFont(font);
|
|
font = NULL; // to be safe...
|
|
TTF_Quit();
|
|
IMG_Quit();
|
|
printf("Quitting SDL_IMG finished!\n");
|
|
SDL_DestroyRenderer(renderer);
|
|
printf("De-initializing renderer finished!\n");
|
|
SDL_DestroyWindow(window);
|
|
printf("De-initializing window finished!\n");
|
|
SDL_Quit();
|
|
printf("Quitting SDL finished!\n");
|
|
printf("De-initializing finished!\n");
|
|
} /* QUIT */
|