Skip to content

Commit c0887d9

Browse files
author
Hugo M. Ruiz-Mireles
committed
Displays proper img size & fish instead of grass
I stopped displaying a grass block from a tutorial and began displaying the fish instead. Made images render in their proper size automatically. Stopped copying assets directory since it is not necessary at the moment
1 parent 6facde9 commit c0887d9

File tree

6 files changed

+49
-31
lines changed

6 files changed

+49
-31
lines changed

CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ add_custom_command(TARGET ${BIN_NAME} POST_BUILD
9292
"$<TARGET_FILE_DIR:${BIN_NAME}>"
9393
)
9494

95-
# Copy assets (res/ directory) to the output bin folder
96-
file(COPY assets DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
97-
9895
# Explicitly tell Visual Studio that headers belong to the project
9996
source_group("Header Files" FILES ${HEADERS})
10097
source_group("Source Files" FILES ${SOURCES})

include/Entity.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Entity
88
Entity(float x, float y, SDL_Texture* texture);
99
float getX();
1010
float getY();
11-
SDL_Texture* getTexture();
11+
SDL_Texture *getTexture();
1212
SDL_Rect getCurrentFrame();
1313
private:
1414
float x;

include/RenderWindow.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
class RenderWindow
77
{
88
public:
9-
RenderWindow(const char* title, int w, int h);
10-
SDL_Texture* loadTexture(const char* filePath);
9+
RenderWindow(const char *title, int w, int h);
10+
SDL_Texture *loadTexture(const char *filePath);
1111
void clear();
1212
void render(Entity& entity);
1313
void display();
1414
~RenderWindow();
1515
private:
16-
SDL_Window* window;
17-
SDL_Renderer* renderer;
16+
SDL_Window *window;
17+
SDL_Renderer *renderer;
1818

1919
};
2020

src/Entity.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ Entity::Entity(float x, float y, SDL_Texture* texture) : x(x), y(y), texture(tex
44
{
55
currentFrame.x = 0;
66
currentFrame.y = 0;
7-
currentFrame.w = 32;
8-
currentFrame.h = 32;
7+
// Automatically detect texture size
8+
int textureW, textureH;
9+
SDL_QueryTexture(texture, NULL, NULL, &textureW, &textureH);
10+
currentFrame.w = textureW;
11+
currentFrame.h = textureH;
912
}
1013
float Entity::getX()
1114
{
@@ -15,7 +18,7 @@ float Entity::getY()
1518
{
1619
return y;
1720
}
18-
SDL_Texture* Entity::getTexture()
21+
SDL_Texture *Entity::getTexture()
1922
{
2023
return texture;
2124
}

src/RenderWindow.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "RenderWindow.hpp"
33

44

5-
RenderWindow::RenderWindow(const char* title, int w, int h) : window(NULL), renderer(NULL)
5+
RenderWindow::RenderWindow(const char *title, int w, int h) : window(NULL), renderer(NULL)
66
{
77
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_SHOWN);
88
if (window == NULL)
@@ -13,9 +13,9 @@ RenderWindow::RenderWindow(const char* title, int w, int h) : window(NULL), rend
1313
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
1414
}
1515

16-
SDL_Texture* RenderWindow::loadTexture(const char* filePath)
16+
SDL_Texture *RenderWindow::loadTexture(const char *filePath)
1717
{
18-
SDL_Texture* texture = NULL;
18+
SDL_Texture *texture = NULL;
1919
texture = IMG_LoadTexture(renderer, filePath);
2020

2121
if (texture == NULL)

src/game.cpp

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
#include <iostream>
2+
#include <string>
23
#include "RenderWindow.hpp"
34
#include "Entity.hpp"
45

5-
int main(int argc, char* args[])
6+
int main(int argc, char *args[])
67
{
78
if (SDL_Init(SDL_INIT_VIDEO) > 0)
89
std::cout << "SDL_Init has failed, SDL ERROR: " << SDL_GetError();
910
if (!(IMG_Init(IMG_INIT_PNG)))
1011
std::cout << "IMG_Init has failed, SDL ERROR: " << SDL_GetError();
1112

12-
RenderWindow window("GAME v1.0", 1080, 600);
13+
RenderWindow window("GAME v1.0", 1280, 720);
1314

14-
const char *assetPath = "../assets/";
15-
// CHANGE FILEPATH FOR TEXTURE LOADER
16-
SDL_Texture* grassTexture = window.loadTexture((std::string(assetPath) + "images/ground_grass_1.png").c_str());
17-
18-
Entity entities[3] = { Entity(0, 568,grassTexture),
19-
Entity(32,568,grassTexture),
20-
Entity(64,568,grassTexture) };
21-
22-
#ifdef CI_BUILD
23-
SDL_Delay(5000);
24-
SDL_Quit();
25-
return 0;
26-
#endif
15+
#ifdef CI_BUILD
16+
SDL_Delay(5000);
17+
SDL_Quit();
18+
return 0;
19+
#endif
2720

21+
const int NUM_FISH_TEXTURES = 9;
22+
SDL_Texture* fishTextures[NUM_FISH_TEXTURES];
23+
24+
fishTextures[0] = window.loadTexture("../assets/images/blue-fish/fish-1.png");
25+
fishTextures[1] = window.loadTexture("../assets/images/blue-fish/fish-2.png");
26+
fishTextures[2] = window.loadTexture("../assets/images/blue-fish/fish-3.png");
27+
fishTextures[3] = window.loadTexture("../assets/images/green-fish/fish-1.png");
28+
fishTextures[4] = window.loadTexture("../assets/images/green-fish/fish-2.png");
29+
fishTextures[5] = window.loadTexture("../assets/images/green-fish/fish-3.png");
30+
fishTextures[6] = window.loadTexture("../assets/images/gold-fish/fish-1.png");
31+
fishTextures[7] = window.loadTexture("../assets/images/gold-fish/fish-2.png");
32+
fishTextures[8] = window.loadTexture("../assets/images/gold-fish/fish-3.png");
33+
34+
Entity fishEntities[8] = { Entity(180, 570, fishTextures[0]),
35+
Entity(408, 570, fishTextures[1]),
36+
Entity(636, 570, fishTextures[2]),
37+
Entity(864, 570, fishTextures[3]),
38+
Entity(1092, 570, fishTextures[4]),
39+
Entity(1092, 410, fishTextures[5]),
40+
Entity(1092, 250, fishTextures[6]),
41+
Entity(1092, 90, fishTextures[7]) };
42+
43+
2844
bool gameRunning = true;
2945

3046
SDL_Event event;
@@ -39,10 +55,12 @@ int main(int argc, char* args[])
3955
}
4056
}
4157
window.clear();
42-
for (int i = 0; i < 3; i++)
58+
// window.render(background);
59+
for (int i = 0; i < 8; i++)
4360
{
44-
window.render(entities[i]);
61+
window.render(fishEntities[i]);
4562
}
63+
4664
window.display();
4765
}
4866

0 commit comments

Comments
 (0)