Fixed collision system! Minor bug fixes

This commit is contained in:
Michael Chen 2018-01-25 17:05:12 +01:00
parent 9982290d77
commit c407253228
5 changed files with 47 additions and 19 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 548 KiB

After

Width:  |  Height:  |  Size: 620 KiB

View File

@ -151,7 +151,6 @@ void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate){
PADDLE_ResetPosition(&(scenery->paddle));
scenery->StartCountdown = 240;
scenery->IsGameOver = false;
scenery->Frames = 0;
if (--(scenery->Lives) <= 0)
GAME_ChangeState(GameOver);
else
@ -246,7 +245,7 @@ void BALL_Initialize(SDL_Renderer * renderer){
Ball BALL_CreateDefault(){
return (Ball) {
.Location = (Vector) {.x = (width / 2) - 15, .y = height - 132 },
.Location = (Vector) {.x = (width / 2) - 15, .y = height - 131 },
.Momentum = (Vector) {.x = 0.0f, .y = BALL_MinSpeed },
.TargetRect = (SDL_Rect) {.x = width / 2 - 15, .y = height - 130, .w = 30, .h = 30 },
.Size = 15.0f,
@ -258,8 +257,8 @@ Ball BALL_CreateDefault(){
}
void BALL_ResetPosition(Ball * obj){
(obj->Location).x = width / 2 - 15;
(obj->Location).y = height - 130;
(obj->Location).x = width / 2 - (obj->Size);
(obj->Location).y = height - 101 - (2 * (obj->Size));
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
(obj->Momentum) = VECTOR_GetScaledDirectionalUnitVector(0.0f, (obj->Speed));
}
@ -310,7 +309,9 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
// printf("Hit corner at %.0f|%.0f!\n", corner.x, corner.y);
// printf("Ball center at %.0f|%.0f!\n", center.x, center.y);
// TODO: Reflection at lot
double lot = VECTOR_GetRotation(VECTOR_GetVectorFromTo(corner, center));
Vector cornerToMid = VECTOR_GetVectorFromTo(corner, center);
if (VECTOR_GetMagnitude(cornerToMid) > (obj->Size)) return false;
double lot = VECTOR_GetRotation(cornerToMid);
double inRotation = fmod(VECTOR_GetRotation(obj->Momentum) + 180.0f, 360.0f);
double outAngle = fmod(lot + (0.5 * (lot - inRotation)), 360.0f);
printf("In: %.2f | Lot: %.2f | Out: %.2f\n", inRotation, lot, outAngle);

View File

@ -9,47 +9,47 @@
#include "gameover.h"
#include "gamestate.h"
#include "breakout.h"
#include "vector.h"
#include "background.h"
#include "main.h"
#define GAMEOVER_TexturePath "assets/images/gameover.png"
#define GAMEOVER_NumbersTexturePath "assets/images/numbers.png"
#define GAMEOVER_ScoreTexturePath "assets/images/yourscore.png"
#define GAMEOVER_UploadTexturePath "assets/images/upload.png"
#define GAMEOVER_HUDScale 16.0f
#define GAMEOVER_Scale 4.0f
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "breakout.h"
#include "vector.h"
#include "background.h"
extern int width, height;
int GAMEOVER_HUDMargin = 5;
SDL_Texture * GAMEOVER_Texture;
SDL_Texture * GAMEOVER_Numbers;
SDL_Texture * GAMEOVER_ScoreTexture;
SDL_Texture * GAMEOVER_UploadTexture;
SDL_Rect * GAMEOVER_NumberRects;
SDL_Rect * GAMEOVER_UploadRects;
SDL_Rect GAMEOVER_TargetRect;
SDL_Rect GAMEOVER_ScoreTargetRect;
SDL_Rect GAMEOVER_HUDScoreTargetRect;
SDL_Rect GAMEOVER_UploadTargetRect;
int * GAMEOVER_Digits;
bool GAMEOVER_IsInit = false;
UploadState GAMEOVER_UploadState = Initial;
void GAMEOVER_Initialize(SDL_Renderer * renderer){
if (!GAMEOVER_IsInit) {
printf("Initializing Gameover...\n");
GAMEOVER_UploadState = Initial;
GAMEOVER_Texture = IMG_LoadTexture(renderer, GAMEOVER_TexturePath);
if (!GAMEOVER_Texture) printf("Gameover Texture couldn't be loaded!\n");
GAMEOVER_Numbers = IMG_LoadTexture(renderer, GAMEOVER_NumbersTexturePath);
if (!GAMEOVER_Numbers) printf("Gameover Numbers couldn't be loaded!\n");
GAMEOVER_ScoreTexture = IMG_LoadTexture(renderer, GAMEOVER_ScoreTexturePath);
if (!GAMEOVER_ScoreTexture) printf("Gameover Score Texture couldn't be loaded!\n");
GAMEOVER_UploadTexture = IMG_LoadTexture(renderer, GAMEOVER_UploadTexturePath);
if (!GAMEOVER_UploadTexture) printf("Gameover Score Texture couldn't be loaded!\n");
int w, h;
SDL_QueryTexture(GAMEOVER_Texture, NULL, NULL, &w, &h);
w /= 2;
@ -70,9 +70,17 @@ void GAMEOVER_Initialize(SDL_Renderer * renderer){
GAMEOVER_NumberRects[7] = (SDL_Rect) {.x = 890, .y = 1335, .w = 407, .h = 665 };
GAMEOVER_NumberRects[8] = (SDL_Rect) {.x = 446, .y = 1335, .w = 442, .h = 665 };
GAMEOVER_NumberRects[9] = (SDL_Rect) {.x = 890, .y = 668, .w = 442, .h = 665 };
GAMEOVER_ScoreTargetRect.x = 0;
GAMEOVER_ScoreTargetRect.y = 450;
GAMEOVER_ScoreTargetRect.h = 183;
GAMEOVER_ScoreTargetRect.w = 1000;
GAMEOVER_UploadTargetRect = (SDL_Rect) {.x = 0, .y = 600, .w = 1000, .h = 200 };
GAMEOVER_UploadRects = malloc(4 * sizeof(SDL_Rect));
if (!GAMEOVER_UploadRects) printf("FATAL: Memory Allocation Failed!\n");
GAMEOVER_UploadRects[0] = (SDL_Rect) {.x = 1, .y = 1, .w = 4634, .h = 732 };
GAMEOVER_UploadRects[1] = (SDL_Rect) {.x = 1, .y = 735, .w = 3981, .h = 734 };
GAMEOVER_UploadRects[2] = (SDL_Rect) {.x = 1, .y = 1471, .w = 3024, .h = 666 };
GAMEOVER_UploadRects[3] = (SDL_Rect) {.x = 3027, .y = 1471, .w = 2391, .h = 666 };
GAMEOVER_HUDScoreTargetRect = (SDL_Rect) {.x = GAMEOVER_HUDMargin, .y = GAMEOVER_HUDMargin, .w = 250, .h = 46 };
GAMEOVER_Digits = malloc(25 * sizeof(int));
printf("Gameover initialized!\n");
@ -100,10 +108,18 @@ void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery){
target.h = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].h) / GAMEOVER_Scale);
target.w = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].w) / GAMEOVER_Scale);
SDL_RenderCopy(renderer, GAMEOVER_Numbers, (GAMEOVER_NumberRects + GAMEOVER_Digits[i]), &target);
xOffset += target.w;
xOffset += target.w - 1;
}
GAMEOVER_DrawHorizontalCenter(renderer, GAMEOVER_UploadTexture, (GAMEOVER_UploadRects + GAMEOVER_UploadState), &GAMEOVER_UploadTargetRect);
} /* GAMEOVER_Draw */
void GAMEOVER_DrawHorizontalCenter(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, SDL_Rect * dstRect){
SDL_Rect target = *dstRect;
target.x = ((width - target.w) / 2);
SDL_RenderCopy(renderer, texture, srcRect, &target);
}
void GAMEOVER_GetDigits(int input, int * digitCount){
int score = input;
int count = 0;
@ -137,7 +153,7 @@ void SCORE_DrawHUD(SDL_Renderer * renderer, Scenery * scenery){
target.h = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].h) / GAMEOVER_HUDScale);
target.w = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].w) / GAMEOVER_HUDScale);
SDL_RenderCopy(renderer, GAMEOVER_Numbers, (GAMEOVER_NumberRects + GAMEOVER_Digits[i]), &target);
xOffset += target.w;
xOffset += target.w - 1;
}
} /* SCORE_DrawHUD */
@ -145,9 +161,12 @@ void GAMEOVER_Deinitialize(){
if (GAMEOVER_IsInit) {
printf("De-initializing Gameover...\n");
free(GAMEOVER_Digits);
free(GAMEOVER_NumberRects);
free(GAMEOVER_UploadRects);
SDL_DestroyTexture(GAMEOVER_Texture);
SDL_DestroyTexture(GAMEOVER_ScoreTexture);
SDL_DestroyTexture(GAMEOVER_Numbers);
SDL_DestroyTexture(GAMEOVER_UploadTexture);
printf("Gameover de-initialized!\n");
GAMEOVER_IsInit = false;
} else

View File

@ -14,9 +14,14 @@
#include "gamestate.h"
#include "main.h"
// Enums
typedef enum uploadStateEnum { Initial = 0, Uploading = 1, Finished = 2 } UploadState;
// Enums
// Prototypes
void GAMEOVER_Initialize(SDL_Renderer * renderer);
void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery);
void GAMEOVER_DrawHorizontalCenter(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, SDL_Rect * dstRect);
void GAMEOVER_GetDigits(int input, int * digitCount);
void SCORE_DrawHUD(SDL_Renderer * renderer, Scenery * scenery);
void GAMEOVER_Deinitialize();

3
main.c
View File

@ -90,6 +90,9 @@ void GAME_ChangeState(GameState state){
}
gameState = state;
switch (gameState) {
case Game:
BALL_ResetPosition(&(scenery.ball));
break;
case Highscores:
HIGHSCORES_ReloadList();
HIGHSCORES_GenerateTexture(renderer);