Physics and some other tweaks
The game window size can now be locked. There's also now a few functions for handling collisions
This commit is contained in:
parent
7784d7dfb5
commit
296a65b062
7
Makefile
7
Makefile
|
@ -16,11 +16,11 @@ test: tests.o logger.o renderwindow.o values.o timestep.o utils.o math.o
|
|||
run_tests: test
|
||||
./build/test
|
||||
|
||||
engine_obj: audio.o entity.o logger.o math.o renderwindow.o timer.o timestep.o utils.o values.o
|
||||
engine_obj: audio.o entity.o logger.o math.o renderwindow.o physics.o timer.o timestep.o utils.o values.o
|
||||
mkdir -p build
|
||||
ld -r $^ -o $(outputDir)/birb2d.o
|
||||
|
||||
engine_lib: audio.o entity.o logger.o math.o renderwindow.o timer.o timestep.o utils.o values.o
|
||||
engine_lib: audio.o entity.o logger.o math.o renderwindow.o physics.o timer.o timestep.o utils.o values.o
|
||||
mkdir -p build
|
||||
g++ -shared $(SDL_FLAGS) -o $(outputDir)/$(LIBFILE) $^
|
||||
|
||||
|
@ -51,6 +51,9 @@ math.o: $(SRCDIR)/math.cpp
|
|||
renderwindow.o: $(SRCDIR)/renderwindow.cpp
|
||||
$(CC) -c $(CFLAGS) $(INCLUDES) $(WarningFlags) $^ -o renderwindow.o
|
||||
|
||||
physics.o: $(SRCDIR)/physics.cpp
|
||||
$(CC) -c $(CFLAGS) $(INCLUDES) $(WarningFlags) $^ -o physics.o
|
||||
|
||||
timer.o: $(SRCDIR)/timer.cpp
|
||||
$(CC) -c $(CFLAGS) $(INCLUDES) $(WarningFlags) $^ -o timer.o
|
||||
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
#include "Utils.hpp"
|
||||
#include "Entity.hpp"
|
||||
|
||||
namespace Birb
|
||||
{
|
||||
namespace Physics
|
||||
{
|
||||
bool RectCollision(Rect rectA, Rect rectB);
|
||||
bool EntityCollision(Entity entityA, Entity entityB);
|
||||
}
|
||||
}
|
|
@ -12,7 +12,7 @@ namespace Birb
|
|||
struct Window
|
||||
{
|
||||
Window();
|
||||
Window(std::string p_title, Vector2int p_window_dimensions, int p_refresh_rate);
|
||||
Window(std::string p_title, Vector2int p_window_dimensions, int p_refresh_rate, bool resizable);
|
||||
|
||||
/* -- Init stuff functions -- */
|
||||
static void InitSDL();
|
||||
|
@ -34,6 +34,8 @@ namespace Birb
|
|||
/* -- Basic window events -- */
|
||||
void SetWindowSize(Vector2int dimensions);
|
||||
void EventTick(SDL_Event event, bool* GameRunning);
|
||||
bool PollEvents();
|
||||
SDL_Event event;
|
||||
/* ------------------------- */
|
||||
|
||||
/* -- Window variables -- */
|
||||
|
@ -42,6 +44,8 @@ namespace Birb
|
|||
SDL_Window* win;
|
||||
SDL_Renderer* renderer = NULL;
|
||||
Vector2int window_dimensions;
|
||||
Vector2int original_window_dimensions;
|
||||
Vector2f window_dimensions_multiplier; ///< Contains the difference between the current and old window dimensions. DO NOT MODIFY!
|
||||
/* ---------------------- */
|
||||
};
|
||||
|
||||
|
@ -58,7 +62,8 @@ namespace Birb
|
|||
{
|
||||
static void DrawEntity(Entity& entity);
|
||||
static void ResetDrawColor();
|
||||
static void DrawRect(SDL_Color color, Rect dimensions);
|
||||
static void DrawRect(SDL_Color color, Rect dimensions); ///< Draw filled rect
|
||||
static void DrawRect(SDL_Color color, Rect dimensions, int width); ///< Draw hollow rect
|
||||
static void DrawCircle(SDL_Color color, Vector2int pos, int radius);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
#include "Physics.hpp"
|
||||
|
||||
namespace Birb
|
||||
{
|
||||
namespace Physics
|
||||
{
|
||||
bool RectCollision(Rect rectA, Rect rectB)
|
||||
{
|
||||
SDL_Rect A = rectA.getSDLRect();
|
||||
SDL_Rect B = rectB.getSDLRect();
|
||||
return SDL_HasIntersection(&A, &B);
|
||||
}
|
||||
|
||||
bool EntityCollision(Entity entityA, Entity entityB)
|
||||
{
|
||||
SDL_Rect A = entityA.rect.getSDLRect();
|
||||
SDL_Rect B = entityB.rect.getSDLRect();
|
||||
return SDL_HasIntersection(&A, &B);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,15 +4,18 @@
|
|||
|
||||
namespace Birb
|
||||
{
|
||||
Window::Window(std::string p_title, Vector2int p_window_dimensions, int p_refresh_rate)
|
||||
:refresh_rate(p_refresh_rate), window_dimensions(p_window_dimensions), win_title(p_title)
|
||||
Window::Window(std::string p_title, Vector2int p_window_dimensions, int p_refresh_rate, bool resizable)
|
||||
:refresh_rate(p_refresh_rate), window_dimensions(p_window_dimensions), original_window_dimensions(p_window_dimensions), win_title(p_title)
|
||||
{
|
||||
/* Create a new window and initialize stuff for it */
|
||||
InitSDL();
|
||||
InitSDL_ttf();
|
||||
InitSDL_image();
|
||||
|
||||
win = SDL_CreateWindow(p_title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, p_window_dimensions.x, p_window_dimensions.y, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
|
||||
if (resizable)
|
||||
win = SDL_CreateWindow(p_title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, p_window_dimensions.x, p_window_dimensions.y, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
|
||||
else
|
||||
win = SDL_CreateWindow(p_title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, p_window_dimensions.x, p_window_dimensions.y, SDL_WINDOW_SHOWN);
|
||||
|
||||
if (win == NULL)
|
||||
Debug::Log("Window failed to init: " + (std::string)SDL_GetError(), Debug::error);
|
||||
|
@ -103,6 +106,8 @@ namespace Birb
|
|||
{
|
||||
SDL_SetWindowSize(win, dimensions.x, dimensions.y);
|
||||
window_dimensions = dimensions;
|
||||
window_dimensions_multiplier.x = (float)window_dimensions.x / (float)original_window_dimensions.x;
|
||||
window_dimensions_multiplier.y = (float)window_dimensions.y / (float)original_window_dimensions.y;
|
||||
}
|
||||
|
||||
void Window::EventTick(SDL_Event event, bool* GameRunning)
|
||||
|
@ -125,6 +130,11 @@ namespace Birb
|
|||
}
|
||||
}
|
||||
|
||||
bool Window::PollEvents()
|
||||
{
|
||||
return (SDL_PollEvent(&event) != 0);
|
||||
}
|
||||
|
||||
SDL_Texture* Resources::LoadTexture(std::string p_filePath)
|
||||
{
|
||||
SDL_Texture* texture = NULL;
|
||||
|
@ -218,6 +228,14 @@ namespace Birb
|
|||
ResetDrawColor();
|
||||
}
|
||||
|
||||
void Render::DrawRect(SDL_Color color, Rect dimensions, int width)
|
||||
{
|
||||
DrawRect(color, Rect(dimensions.x, dimensions.y, dimensions.w, width)); /* Top */
|
||||
DrawRect(color, Rect(dimensions.x, dimensions.y + dimensions.h - width, dimensions.w, width)); /* Bottom */
|
||||
DrawRect(color, Rect(dimensions.x, dimensions.y, width, dimensions.h)); /* Left */
|
||||
DrawRect(color, Rect(dimensions.x + dimensions.w - width, dimensions.y, width, dimensions.h)); /* Right */
|
||||
}
|
||||
|
||||
void Render::DrawCircle(SDL_Color color, Vector2int pos, int radius)
|
||||
{
|
||||
Uint32 uColor = (255<<24) + (int(color.b)<<16) + (int(color.g)<<8) + int(color.r);;
|
||||
|
|
Loading…
Reference in New Issue