Game Layout and design changed

This commit is contained in:
Michael Chen 2018-01-15 20:28:47 +01:00
parent cadfbdc0fa
commit 0dd20c4514
6 changed files with 69 additions and 37 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 151 KiB

After

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 314 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 526 KiB

View File

@ -22,17 +22,17 @@ SDL_Texture * BLOCK_Texture;
SDL_Rect * BALL_SourceRects;
SDL_Rect * PADDLE_SourceRects;
SDL_Rect * BLOCK_SourceRects;
Ball ball;
Paddle paddle;
Block * blocks;
int BlockCount = 60; // Move to scenery
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
double BALL_Speed = 15.0f;
int PADDLE_Speed = 10;
bool BREAKOUT_IsInit = false;
bool BALL_IsInit = false;
bool PADDLE_IsInit = false;
bool BLOCK_IsInit = false;
Ball ball;
Paddle paddle;
Block * blocks;
int BlockCount = 60; // Move to scenery
double BALL_Speed = 15.0f;
int PADDLE_Speed = 10;
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
if (!BREAKOUT_IsInit) {
@ -50,8 +50,15 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
for (int y = 0; y < 6; y++) {
index = 10 * y;
for (int x = 0; x < 10; x++) {
blocks[x + index] = BLOCK_CreateDefault();
blocks[x + index].TargetRect = (SDL_Rect) {.x = ((192 * x) + 4), .y = ((96 * y) + 2), .w = 184, .h = 92 };
if (x + y == 0) {
blocks[x + index] = BLOCK_CreateDefault();
blocks[x + index].TargetRect = (SDL_Rect) {.x = 1, 1, .w = 184, .h = 92 };
blocks[x + index].TextureIndex = y + x;
} else {
blocks[x + index] = BLOCK_CreateDefault();
blocks[x + index].TargetRect = (SDL_Rect) {.x = ((192 * x) + 4), .y = ((96 * y) + 2), .w = 184, .h = 92 };
blocks[x + index].TextureIndex = y + x;
}
}
}
printf("Game initialized!\n");
@ -59,6 +66,7 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
} else printf("Game is already initialized!\n");
} /* BREAKOUT_INITIALIZE */
// This Function is obsolete! Do not use it!
void BREAKOUT_ChangeSize(int width, int height){
BREAKOUT_BoxWidth = width;
BREAKOUT_BoxHeight = height;
@ -167,6 +175,7 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
(obj->Momentum).x = -(obj->Momentum).x;
if (xMid)
(obj->Momentum).y = -(obj->Momentum).y;
if (yMid && xMid) printf("WARNING: Ball is completely inside block!\n");
if (yMid || xMid) { // Ball collides with Edge
} else { // Ball collides with corner
/*
@ -274,7 +283,7 @@ void PADDLE_Initialize(SDL_Renderer * renderer){
if (!PADDLE_Texture) printf("Paddle texture failed to load!\n");
PADDLE_SourceRects = (SDL_Rect *)malloc(1 * sizeof(SDL_Rect));
if (!PADDLE_SourceRects) printf("FATAL! Memory allocation failed!\n");
PADDLE_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 512, .h = 512 };
PADDLE_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 1000, .h = 100 };
PADDLE_MoveLeftKeys = (Uint8 *)malloc(2 * sizeof(Uint8));
if (!PADDLE_MoveLeftKeys) printf("FATAL! Memory allocation failed!\n");
PADDLE_MoveRightKeys = (Uint8 *)malloc(2 * sizeof(Uint8));
@ -301,9 +310,9 @@ Paddle PADDLE_CreateDefault(){
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));
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(Uint8 * keystate, Uint8 * keyArray){
@ -346,24 +355,42 @@ void PADDLE_Deinitialize(){
void BLOCK_Initialize(SDL_Renderer * renderer){
if (!BLOCK_IsInit) {
printf("Initializing Block...\n");
BLOCK_Texture = IMG_LoadTexture(renderer, "assets/images/blocks_debug.png");
BLOCK_Texture = IMG_LoadTexture(renderer, "assets/images/spritesheet.png");
if (!BLOCK_Texture) printf("Block texture failed to load!\n");
BLOCK_SourceRects = (SDL_Rect *)malloc(BLOCK_TextureCount * sizeof(SDL_Rect));
if (!BLOCK_SourceRects) printf("FATAL! Memory allocation failed!\n");
for (int i = 0; i < BLOCK_TextureCount; i++) {
// TODO: All textures!
BLOCK_SourceRects[i] = (SDL_Rect) {.x = 0, .y = 500 * i * 0, .w = 1000, .h = 500 };
}
BLOCK_SourceRects[0] = (SDL_Rect) {.x = 2000, .y = 1500, .w = 1000, .h = 500 };
BLOCK_SourceRects[1] = (SDL_Rect) {.x = 2000, .y = 2000, .w = 1000, .h = 500 };
BLOCK_SourceRects[2] = (SDL_Rect) {.x = 2000, .y = 2500, .w = 1000, .h = 500 };
BLOCK_SourceRects[3] = (SDL_Rect) {.x = 0, .y = 3000, .w = 1000, .h = 500 };
BLOCK_SourceRects[4] = (SDL_Rect) {.x = 1000, .y = 3000, .w = 1000, .h = 500 };
BLOCK_SourceRects[5] = (SDL_Rect) {.x = 2000, .y = 3000, .w = 1000, .h = 500 };
BLOCK_SourceRects[6] = (SDL_Rect) {.x = 0, .y = 3500, .w = 1000, .h = 500 };
BLOCK_SourceRects[7] = (SDL_Rect) {.x = 1000, .y = 3500, .w = 1000, .h = 500 };
BLOCK_SourceRects[8] = (SDL_Rect) {.x = 2000, .y = 3500, .w = 1000, .h = 500 };
BLOCK_SourceRects[9] = (SDL_Rect) {.x = 0, .y = 500, .w = 1000, .h = 500 };
BLOCK_SourceRects[10] = (SDL_Rect) {.x = 2000, .y = 0, .w = 1000, .h = 500 };
BLOCK_SourceRects[11] = (SDL_Rect) {.x = 0, .y = 1000, .w = 1000, .h = 500 };
BLOCK_SourceRects[12] = (SDL_Rect) {.x = 0, .y = 1500, .w = 1000, .h = 500 };
BLOCK_SourceRects[13] = (SDL_Rect) {.x = 1000, .y = 0, .w = 1000, .h = 500 };
BLOCK_SourceRects[14] = (SDL_Rect) {.x = 1000, .y = 500, .w = 1000, .h = 500 };
BLOCK_SourceRects[15] = (SDL_Rect) {.x = 1000, .y = 1000, .w = 1000, .h = 500 };
BLOCK_SourceRects[16] = (SDL_Rect) {.x = 1000, .y = 1500, .w = 1000, .h = 500 };
BLOCK_SourceRects[17] = (SDL_Rect) {.x = 0, .y = 2000, .w = 1000, .h = 500 };
BLOCK_SourceRects[18] = (SDL_Rect) {.x = 1000, .y = 2000, .w = 1000, .h = 500 };
BLOCK_SourceRects[19] = (SDL_Rect) {.x = 0, .y = 2500, .w = 1000, .h = 500 };
BLOCK_SourceRects[20] = (SDL_Rect) {.x = 1000, .y = 2500, .w = 1000, .h = 500 };
BLOCK_SourceRects[21] = (SDL_Rect) {.x = 0, .y = 0, .w = 1000, .h = 500 };
BLOCK_SourceRects[22] = (SDL_Rect) {.x = 2000, .y = 500, .w = 1000, .h = 500 };
BLOCK_SourceRects[23] = (SDL_Rect) {.x = 2000, .y = 1000, .w = 1000, .h = 500 };
printf("Block initialized!\n");
BLOCK_IsInit = true;
} else printf("Block is already initialized!\n");
} /* PADDLE_Initialize */
Block BLOCK_CreateDefault() {
int defaultpaddlewidth = 300;
return (Block) {
.TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = 150, .h = 75 },
.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 100, .h = 50 },
.TextureIndex = (rand() % BLOCK_TextureCount),
.HP = 1
}; // Objekt für die Eigenschaften des Balls

39
main.c
View File

@ -23,6 +23,7 @@
void INITIALIZE();
void QUIT();
void HandleSDLEvents();
void DrawText(SDL_Renderer * renderer, const char * text);
void DrawBackground(SDL_Renderer * renderer);
void printFontStyle(TTF_Font * ffont);
@ -52,23 +53,7 @@ int main(int argc, char * args[]){
gameState = readIntFromIO("W"ae "hle einen Spielbereich aus, den du testen m"oe "chtest:", "Fehlerhafte Eingabe!\n", "%d ist kein g"ue "ltiger Spielbereich!\n", 1, 5);
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;
}
}
HandleSDLEvents();
keystate = SDL_GetKeyboardState(NULL);
DrawBackground(renderer);
switch (gameState) {
@ -90,6 +75,26 @@ int main(int argc, char * args[]){
return 0;
} /* main */
void HandleSDLEvents(){
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;
}
}
}
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);