Skip to content

Commit

Permalink
I forgot what I cahnged
Browse files Browse the repository at this point in the history
  • Loading branch information
durkisneer1 committed Nov 13, 2023
1 parent 2a54b42 commit 994e06d
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 78 deletions.
1 change: 0 additions & 1 deletion DurkGame/DurkGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "Font.hpp"
#include "Character.hpp"
#include "Globals.hpp"
#include "Surface.hpp"
#include "RenderWindow.hpp"


Expand Down
2 changes: 0 additions & 2 deletions DurkGame/DurkGame.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@
<ClInclude Include="Character.hpp" />
<ClInclude Include="Clock.hpp" />
<ClInclude Include="RenderWindow.hpp" />
<ClInclude Include="Surface.hpp" />
<ClInclude Include="DurkGame.hpp" />
<ClInclude Include="Font.hpp" />
<ClInclude Include="framework.h" />
Expand All @@ -171,7 +170,6 @@
<ClCompile Include="character.cpp" />
<ClCompile Include="clock.cpp" />
<ClCompile Include="render_window.cpp" />
<ClCompile Include="surface.cpp" />
<ClCompile Include="durk_game.cpp" />
<ClCompile Include="font.cpp" />
<ClCompile Include="input.cpp" />
Expand Down
6 changes: 0 additions & 6 deletions DurkGame/DurkGame.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@
<ClInclude Include="Math.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Surface.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RenderWindow.hpp">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -83,9 +80,6 @@
<ClCompile Include="texture.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="surface.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="render_window.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
1 change: 0 additions & 1 deletion DurkGame/Font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <SDL_ttf.h>
#include <iostream>
#include "Texture.hpp"
#include "Surface.hpp"
#include "RenderWindow.hpp"


Expand Down
2 changes: 1 addition & 1 deletion DurkGame/Globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@


extern const dk::math::Vector2 WIN_SIZE;
extern const float GRAVITY;
extern float GRAVITY;
7 changes: 7 additions & 0 deletions DurkGame/Math.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cmath>
#include <algorithm>


namespace dk {
Expand All @@ -20,7 +21,13 @@ namespace dk {
Vector2 operator*(float scalar) const;
Vector2 operator/(float scalar) const;
Vector2 operator+(const Vector2& other) const;
Vector2 operator-(const Vector2& other) const;
Vector2 operator+=(const Vector2& other);
};

Vector2 clampVec(Vector2 vec, Vector2 min, Vector2 max);

template <class digit>
digit clamp(digit val, digit min, digit max);
}
}
2 changes: 2 additions & 0 deletions DurkGame/Rect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <SDL.h>
#include "Math.hpp"
#include "Globals.hpp"


namespace dk {
Expand All @@ -14,6 +15,7 @@ namespace dk {
dk::math::Vector2 getPos();
dk::math::Vector2 getSize();
bool collidePoint(dk::math::Vector2 pos);
void clamp(dk::math::Vector2 min = {}, dk::math::Vector2 max = WIN_SIZE);

void setCenter(dk::math::Vector2 pos);
void setTopLeft(dk::math::Vector2 pos);
Expand Down
26 changes: 0 additions & 26 deletions DurkGame/Surface.hpp

This file was deleted.

6 changes: 4 additions & 2 deletions DurkGame/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <iostream>
#include "Math.hpp"
#include "Rect.hpp"
#include "Surface.hpp"
#include "RenderWindow.hpp"


Expand All @@ -14,7 +13,9 @@ namespace dk {
public:
Texture() = default;
Texture(dk::RenderWindow& window, const char* fileDir);
Texture(dk::RenderWindow& window, dk::Surface& surface);
Texture(dk::RenderWindow& window, dk::math::Vector2 size, SDL_Color color);
Texture(dk::RenderWindow& window, SDL_Surface* surface);
Texture(dk::RenderWindow& window, SDL_Texture* texture);
~Texture() { if (texture) SDL_DestroyTexture(texture); }

[[nodiscard]] dk::math::Vector2 getSize() const;
Expand All @@ -25,6 +26,7 @@ namespace dk {
void scaleBy(float scale);
void blit(dk::Rect rect);
void blitAngle(dk::Rect rect, float angle);
void query();
private:
dk::RenderWindow& window;
SDL_Texture* texture = nullptr;
Expand Down
13 changes: 7 additions & 6 deletions DurkGame/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,32 @@ namespace dk {
}

dk::Texture Font::render(const char* text, bool antialias, SDL_Color color, int wrapLength) {
dk::Surface surface;
SDL_Surface* surface;

if (antialias) {
if (wrapLength > 0) {
surface.set(TTF_RenderUTF8_Blended_Wrapped(font, text, color, wrapLength));
surface = TTF_RenderUTF8_Blended_Wrapped(font, text, color, wrapLength);
}
else {
surface.set(TTF_RenderUTF8_Blended(font, text, color));
surface = TTF_RenderUTF8_Blended(font, text, color);
}
}
else {
if (wrapLength > 0) {
surface.set(TTF_RenderText_Solid_Wrapped(font, text, color, wrapLength));
surface = TTF_RenderText_Solid_Wrapped(font, text, color, wrapLength);
}
else {
surface.set(TTF_RenderText_Solid(font, text, color));
surface = TTF_RenderText_Solid(font, text, color);
}
}

if (!surface.get()) {
if (surface == nullptr) {
std::cout << "Failed to render text: " << TTF_GetError() << std::endl;
exit(3);
}

dk::Texture texture(window, surface);
SDL_FreeSurface(surface);

return texture;
}
Expand Down
17 changes: 17 additions & 0 deletions DurkGame/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,27 @@ namespace dk {
return { x + other.x, y + other.y };
}

Vector2 Vector2::operator-(const Vector2& other) const {
return { x - other.x, y - other.y };
}

Vector2 Vector2::operator+=(const Vector2& other) {
x += other.x;
y += other.y;
return *this;
}

Vector2 clampVec(Vector2 vec, Vector2 min, Vector2 max) {
vec.x = clamp(vec.x, min.x, max.x);
vec.y = clamp(vec.y, min.y, max.y);
return vec;
}

template <class digit>
digit clamp(digit val, digit min, digit max) {
if (min > max) return val;
val = std::min(max, std::max(min, val));
return val;
}
}
}
8 changes: 8 additions & 0 deletions DurkGame/rect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ namespace dk {
return (pos.x >= x && pos.x <= x + w && pos.y >= y && pos.y <= y + h);
}

void Rect::clamp(dk::math::Vector2 min, dk::math::Vector2 max) {
if ((max.x - min.x < this->w) || max.y - min.y < this->h) {
return;
}
setTopLeft(clampVec(getTopLeft(), min, max));
setBottomRight(clampVec(getBottomRight(), min, max));
}

void Rect::setCenter(dk::math::Vector2 pos) {
x = pos.x - w / 2;
y = pos.y - h / 2;
Expand Down
28 changes: 0 additions & 28 deletions DurkGame/surface.cpp

This file was deleted.

35 changes: 30 additions & 5 deletions DurkGame/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,41 @@ namespace dk {
loadTextureFile(fileDir);
}

Texture::Texture(dk::RenderWindow& window, dk::Surface& surface)
Texture::Texture(dk::RenderWindow& window, dk::math::Vector2 size, SDL_Color color)
: window(window) {
texture = SDL_CreateTextureFromSurface(window.getRenderer(), surface.get());
SDL_Surface* surface = SDL_CreateRGBSurface(0, (int)size.x, (int)size.y, 32, 0, 0, 0, 0);
if (surface == nullptr) {
std::cout << "SDL_CreateRGBSurface Error: " << SDL_GetError() << std::endl;
exit(3);
}

SDL_FillRect(surface, nullptr, SDL_MapRGB(surface->format, color.r, color.g, color.b));
texture = SDL_CreateTextureFromSurface(window.getRenderer(), surface);
if (texture == nullptr) {
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
}

SDL_FreeSurface(surface);
query();
}

Texture::Texture(dk::RenderWindow& window, SDL_Surface* surface)
: window(window) {
texture = SDL_CreateTextureFromSurface(window.getRenderer(), surface);
if (!texture) {
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
exit(3);
}

query();
}

Texture::Texture(dk::RenderWindow& window, SDL_Texture* texture)
: window(window), texture(texture) {
query();
}

void Texture::query() {
int w, h;
SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
this->rect = { 0, 0, w, h };
Expand All @@ -28,9 +55,7 @@ namespace dk {
exit(3);
}

int w, h;
SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
this->rect = { 0, 0, w, h };
query();
}

void Texture::blit(dk::Rect dstRect) {
Expand Down

0 comments on commit 994e06d

Please sign in to comment.