Added dll for SDL2_image and implemented top level functions for ball.

This commit is contained in:
Michael Chen 2018-01-10 23:40:17 +01:00
parent 7f36f6dd14
commit 71b4993a6b
7 changed files with 64 additions and 14 deletions

1
.gitignore vendored
View File

@ -12,3 +12,4 @@ sdl2-config
*.exe *.exe
!bhi.exe !bhi.exe
.tags* .tags*
*.txt

View File

@ -12,5 +12,4 @@ all:
$(compiler) $(warningLevel) $(includes) $(sources) $(linker) $(libs) $(args) $(dir)\$(target) $(compiler) $(warningLevel) $(includes) $(sources) $(linker) $(libs) $(args) $(dir)\$(target)
run: run:
cd $(dir) && \ cd $(dir) && $(target)
$(target)

View File

Before

Width:  |  Height:  |  Size: 151 KiB

After

Width:  |  Height:  |  Size: 151 KiB

Binary file not shown.

View File

@ -1,4 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h> #include <stdbool.h>
#include <math.h> #include <math.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
@ -14,25 +16,69 @@
int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight; int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight;
SDL_Texture * BALL_Texture; SDL_Texture * BALL_Texture;
SDL_Rect * BALL_SourceRects;
Ball ball;
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
printf("Initializing Game...\n"); printf("Initializing Game...\n");
srand(time(NULL));
BREAKOUT_BoxWidth = width; BREAKOUT_BoxWidth = width;
BREAKOUT_BoxHeight = height; BREAKOUT_BoxHeight = height;
BALL_Texture = IMG_LoadTexture(renderer, "assets/images/ball.png");
if (!BALL_Texture) printf("Ball texture cannot be loaded!\n"); if (!BALL_Texture) printf("Ball texture cannot be loaded!\n");
printf("Game initialized!\n"); printf("Game initialized!\n");
} }
void BREAKOUT_GAMELOOP(Uint8 * keystate){ void BREAKOUT_Update(Uint8 * keystate){
BALL_Update(&ball);
} }
void BALL_DRAW(Ball * ball){ void BREAKOUT_Draw(SDL_Renderer * renderer){
BALL_Draw(renderer, &ball);
} }
void BREAKOUT_DEINITIALIZE(SDL_Renderer * renderer, int width, int height){ void BREAKOUT_DEINITIALIZE(){
printf("De-initializing Game...\n"); printf("De-initializing Game...\n");
SDL_DestroyTexture(BALL_Texture); SDL_DestroyTexture(BALL_Texture);
printf("Game de-initialized!\n"); printf("Game de-initialized!\n");
} }
void BALL_Initialize(SDL_Renderer * renderer){
printf("Initializing Ball...\n");
BALL_Texture = IMG_LoadTexture(renderer, "assets/images/ball.png");
if (!BALL_Texture) printf("Ball texture failed to load!\n");
BALL_SourceRects = (SDL_Rect *)malloc(1 * sizeof(SDL_Rect));
BALL_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 512, .h = 512 };
ball = BALL_CreateDefault();
printf("Ball initialized!\n");
}
Ball BALL_CreateDefault(){
double rotation = (double)(rand() % 360);
return (Ball) {
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 },
.Momentum = getScaledDirectionalUnitVector(rotation, 5),
.TargetRect = (SDL_Rect) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2, .w = 50, .h = 50 },
.Size = 50.0f,
.Rotation = rotation,
.RotationValue = 2,
.TextureIndex = 0
}; // Objekt für die Eigenschaften des Balls
}
void BALL_Draw(SDL_Renderer * renderer, Ball * obj){
SDL_RenderCopyEx(renderer, BALL_Texture, BALL_SourceRects + (obj->TextureIndex), &(obj->TargetRect), obj->Rotation, NULL, SDL_FLIP_NONE);
}
void BALL_Update(Ball * obj){
obj->Location = vectorAdd(obj->Location, obj->Momentum);
(obj->TargetRect).x = (int)round((obj->Location).x);
(obj->TargetRect).y = (int)round((obj->Location).y);
}
void BALL_DestroyObject(Ball * obj){
}
void BALL_Deinitialize(){
printf("De-initializing Ball...\n");
printf("Ball de-initialized!\n");
}

View File

@ -26,8 +26,15 @@ typedef struct blockStruct {
// Prototypes // Prototypes
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height); void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height);
void BREAKOUT_GAMELOOP(Uint8 * keystate); void BREAKOUT_Update(Uint8 * keystate);
void BREAKOUT_DEINITIALIZE(SDL_Renderer * renderer, int width, int height); void BREAKOUT_Draw(SDL_Renderer * renderer);
void BREAKOUT_DEINITIALIZE();
void BALL_Initialize(SDL_Renderer * renderer);
Ball BALL_CreateDefault();
void BALL_Draw(SDL_Renderer * renderer, Ball * obj);
void BALL_Update(Ball * obj);
void BALL_DestroyObject(Ball * obj);
void BALL_Deinitialize();
// End Prototypes // End Prototypes
#endif // __breakout_h__ #endif // __breakout_h__

7
main.c
View File

@ -64,6 +64,7 @@ int main(int argc, char * args[]){
void GAMELOOP() { void GAMELOOP() {
keystate = SDL_GetKeyboardState(NULL); keystate = SDL_GetKeyboardState(NULL);
BREAKOUT_Update(keystate);
} /* GAMELOOP */ } /* GAMELOOP */
void mousePress(SDL_MouseButtonEvent b){ // Debug prop void mousePress(SDL_MouseButtonEvent b){ // Debug prop
@ -101,13 +102,9 @@ void windowChanged(SDL_WindowEvent b){ // Debug prop
} }
void DrawFrame(){ void DrawFrame(){
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
// BALL_DRAW(); BREAKOUT_Draw(renderer);
SDL_RenderPresent(renderer);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }