Skip to content

Commit

Permalink
Inventory with custom items and item count
Browse files Browse the repository at this point in the history
  • Loading branch information
arbeiter committed Aug 17, 2022
1 parent eb3f15a commit f8d4e07
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 28 deletions.
4 changes: 2 additions & 2 deletions code/clonegame/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using namespace std;
Game::Game()
:mWindow(nullptr)
,mRenderer(nullptr)
,inventory(nullptr)
,inventory()
,mTicksCount(0)
,mPlayer(0,0, "")
{
Expand Down Expand Up @@ -68,7 +68,7 @@ bool Game::initialize() {
}

loadFonts();
inventory = Inventory(mRenderer);
inventory = Inventory(mRenderer, mFont);
return true;
}

Expand Down
65 changes: 59 additions & 6 deletions code/clonegame/inventory.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
#include "inventory.h"

// TODO: RefreshDisplay doesn't update the size of the items
// TODO: An inventory should always a transparent overlay
// TODO: An asset manager that reads a list of textures
// TODO: Click and drag behavior, hook into SDL events inside the class
// TODO: Add orientation overlay: Top left or top right
// Move font rendering to a Text class
// Add item equipped tray

void Inventory::loadItemStats() {
itemStats.clear();
itemStats.push_back({3, "Bag"});
itemStats.push_back({2, "Belt"});
itemStats.push_back({10, "Helm"});
itemStats.push_back({2, "Armor"});
itemStats.push_back({1, "Iron Boot"});
itemStats.push_back({4, "Iron Helment"});
}

void Inventory::draw() {
refreshDisplay();
// an inventory is always centered on the screen
drawChrome();

}

void Inventory::refreshDisplay() {
Expand All @@ -33,14 +43,13 @@ void Inventory::drawChrome() {
SDL_RenderFillRect(mRenderer, &srcrect);
SDL_SetRenderDrawBlendMode(mRenderer, SDL_BLENDMODE_NONE);
drawItems((int)(width/3), (int)(height/3));

}

void Inventory::drawItems(int w, int h) {
int start_x = w;
int start_y = h;

int width = 40;
int width = 80;
int abs_width = start_x;
int abs_height= start_y;

Expand All @@ -49,6 +58,7 @@ void Inventory::drawItems(int w, int h) {
int max_x = start_x + num_items_per_row * width;
int max_y = start_y + num_items_per_col* width;

int num_items = itemStats.size();
for(int i = 0; i < num_items; i++) {
SDL_Rect srcrect;
int active_row = i / num_items_per_row;
Expand All @@ -60,7 +70,11 @@ void Inventory::drawItems(int w, int h) {
srcrect.y = (start_pos_y + h);
srcrect.w = width;
srcrect.h = width;
drawItem(srcrect, width, i);
}
}

void Inventory::drawItem(SDL_Rect &srcrect, int width, int item_idx) {
SDL_Rect inner_rect;
inner_rect.x = srcrect.x + 3;
inner_rect.y = srcrect.y + 3;
Expand All @@ -69,6 +83,45 @@ void Inventory::drawItems(int w, int h) {

SDL_SetRenderDrawColor(mRenderer, 1, 1, 1, 1);
SDL_RenderFillRect(mRenderer, &inner_rect);
texManager->ClipTexture(bitmapTex, 0, 0, tex_w, tex_h, inner_rect.x + 1, inner_rect.y + 2, width - 8, width - 7);

// given the item's index
// item height = (int)(tex_h / num_items)
// item width = (int)(tex_w)
int num_items = itemStats.size();
int abs_w = tex_w;
int abs_h = 32;
int tex_offset_x = 0;
int tex_offset_y = item_idx * abs_h;

texManager->ClipTexture(bitmapTex,
tex_offset_x, tex_offset_y, abs_w, abs_h,
inner_rect.x + 1, inner_rect.y + 2, width - 8, width - 7);
drawCount(srcrect, itemStats[item_idx].itemCount);
}

void Inventory::drawCount(SDL_Rect &srcrect, int count) {
int num_segments = 6;
int width = srcrect.w / num_segments;
int height = srcrect.h / num_segments;
int start_pos_x = srcrect.x + (num_segments - 1) * width;
int start_pos_y = srcrect.y + (num_segments - 1) * width;
SDL_Rect dest = { start_pos_x, start_pos_y, width, height};

if(count == 1) {
return;
}

SDL_Surface* text;
string pos = to_string(count);

SDL_Color color = {255, 255, 255};
text = TTF_RenderText_Solid(font, pos.c_str(), color);
if(!text) {
cout << "Failed to render text" << TTF_GetError() << endl;
}

SDL_Texture* text_texture;
text_texture = SDL_CreateTextureFromSurface(mRenderer, text);
SDL_RenderCopy(mRenderer, text_texture, NULL, &dest);
SDL_FreeSurface(text);
}
38 changes: 19 additions & 19 deletions code/clonegame/inventory.h
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
#pragma once
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_ttf.h"
#include <iostream>
#include <ostream>
#include "textureManager.h"
#include <vector>
using namespace std;

struct ItemStats {
int itemCount;
string itemName;
};

class Inventory {
public:
Inventory(SDL_Renderer *renderer)
: mRenderer{renderer}, width(3440), height(1440), num_items(60)
Inventory() = default;
Inventory(SDL_Renderer *renderer, TTF_Font *mFont)
: mRenderer{renderer}, font(mFont), width(3440), height(1440)
{
texManager = new TextureManager(renderer);
bitmapTex = texManager->LoadTexture("./res/collectable_jam.png");

bitmapTex = texManager->LoadTexture("./res/final_collectables_transparent.png");
loadItemStats();
int wi, hi;
SDL_QueryTexture(bitmapTex, NULL, NULL, &wi, &hi);
tex_w = wi;
tex_h = hi;
}

void draw();
void refreshDisplay();
void loadItemStats();
void drawItem(SDL_Rect &srcrect, int width, int item_idx);
void drawCount(SDL_Rect &srcrect, int count);

private:
void drawChrome();
void drawItems(int w, int h);
void drawItem();
SDL_Renderer *mRenderer;
int width;
int height;
int num_items;
TextureManager *texManager;
SDL_Texture *bitmapTex;
int tex_w;
int tex_h;
};

class InventoryItem {
public:
InventoryItem(SDL_Renderer *renderer)
: mRenderer{renderer} {}
private:
void drawChrome();
void drawInterior();
int padding;
string item_texture;
string item_count;
string border_texture;
SDL_Renderer *mRenderer;
std::vector<ItemStats> itemStats;
TTF_Font* font;
};
2 changes: 1 addition & 1 deletion code/clonegame/textureManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TextureManager {
{}

void RenderTexture(SDL_Texture *tex, int x, int y);
void RenderTexture(SDL_Texture *tex, int x, int y, int w, int h);
void RenderTexture(SDL_Texture *tex, int x, int y, int w, int h);
SDL_Texture* LoadTexture(const char* texture);
void ClipTexture(SDL_Texture *tex, float x, float y, int w, int h, int p_x, int p_y, int p_w, int p_h);
SDL_Texture* preloadImage(std::string name);
Expand Down
Binary file added code/res/collectables.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added code/res/final_collectables.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added code/res/final_collectables_transparent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f8d4e07

Please sign in to comment.