added inventory testing buttons I to add items O to delete last items in inventory and p to get info about inventory

This commit is contained in:
Альберт Гадиев 2025-03-01 19:04:49 +06:00
parent a8ab5015b9
commit 880caecd97

209
main.cpp
View File

@ -112,6 +112,12 @@ namespace ZL
// Add AudioPlayer instance // Add AudioPlayer instance
std::unique_ptr<AudioPlayer> audioPlayer; std::unique_ptr<AudioPlayer> audioPlayer;
// Добавляем структуры для иконок инвентаря
VertexDataStruct inventoryIconMesh;
VertexRenderStruct inventoryIconMeshMutable;
const float INVENTORY_ICON_SIZE = 32.0f; // Размер иконки в пикселях
const float INVENTORY_MARGIN = 10.0f; // Отступ от края экрана
} }
static SDL_Window* window = NULL; static SDL_Window* window = NULL;
@ -213,6 +219,18 @@ namespace ZL
} }
} }
// Отрисовка иконок инвентаря
const auto& inventory = ReturnInventory();
for (size_t i = 0; i < inventory.size(); ++i) {
renderer.PushMatrix();
float xPos = Env::width - GameObjects::INVENTORY_MARGIN - GameObjects::INVENTORY_ICON_SIZE;
float yPos = GameObjects::INVENTORY_MARGIN + i * (GameObjects::INVENTORY_ICON_SIZE + GameObjects::INVENTORY_MARGIN);
renderer.TranslateMatrix(Vector3f{xPos, yPos, 0.0f});
glBindTexture(GL_TEXTURE_2D, inventory[i].texture->getTexID());
renderer.DrawVertexRenderStruct(GameObjects::inventoryIconMeshMutable);
renderer.PopMatrix();
}
renderer.PopMatrix(); renderer.PopMatrix();
renderer.PopProjectionMatrix(); renderer.PopProjectionMatrix();
@ -393,14 +411,14 @@ namespace ZL
ZL::AddItemToInventory("RoomCeramics", roomTexturePtr); ZL::AddItemToInventory("RoomCeramics", roomTexturePtr);
ZL::AddItemToInventory("Cone", coneTexturePtr); ZL::AddItemToInventory("Cone", coneTexturePtr);
std::cout << "Before removal:\n"; // std::cout << "Before removal:\n";
ZL::PrintInventory(); ZL::PrintInventory();
// Удаляем "Cone" из инвентаря // Удаляем "Cone" из инвентаря
ZL::RemoveItemFromInventory("Cone"); // ZL::RemoveItemFromInventory("Cone");
std::cout << "\nAfter removal:\n"; // std::cout << "\nAfter removal:\n";
ZL::PrintInventory(); // ZL::PrintInventory();
// Initialize audio player and start background music // Initialize audio player and start background music
GameObjects::audioPlayer = std::make_unique<AudioPlayer>(); GameObjects::audioPlayer = std::make_unique<AudioPlayer>();
@ -408,6 +426,15 @@ namespace ZL
GameObjects::audioPlayer->playMusic("Symphony No.6 (1st movement).ogg"); GameObjects::audioPlayer->playMusic("Symphony No.6 (1st movement).ogg");
} }
// Инициализация меша иконки инвентаря
GameObjects::inventoryIconMesh = CreateRect2D(
{0.0f, 0.0f},
{GameObjects::INVENTORY_ICON_SIZE/2, GameObjects::INVENTORY_ICON_SIZE/2},
0.5f
);
GameObjects::inventoryIconMeshMutable.AssignFrom(GameObjects::inventoryIconMesh);
GameObjects::inventoryIconMeshMutable.RefreshVBO();
/// ///
} }
@ -427,98 +454,124 @@ namespace ZL
} }
void update() { void update() {
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) { if (event.type == SDL_QUIT) {
ExitGameLoop = true; ExitGameLoop = true;
} }
if (event.type == SDL_MOUSEBUTTONDOWN) { if (event.type == SDL_MOUSEBUTTONDOWN) {
static int x = 0; static int x = 0;
GameObjects::bx.Interpolate(x); GameObjects::bx.Interpolate(x);
x = x + 2; x = x + 2;
} }
if (event.type == SDL_MOUSEWHEEL) { if (event.type == SDL_MOUSEWHEEL) {
static const float zoomstep = 1.0f; static const float zoomstep = 1.0f;
if (event.wheel.y > 0) { if (event.wheel.y > 0) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> zoom // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> zoom
Env::zoom -= zoomstep; Env::zoom -= zoomstep;
} }
else if (event.wheel.y < 0) { else if (event.wheel.y < 0) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> zoom // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> zoom
Env::zoom += zoomstep; Env::zoom += zoomstep;
} }
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> zoom, <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> zoom, <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (Env::zoom < zoomstep) { if (Env::zoom < zoomstep) {
Env::zoom = zoomstep; Env::zoom = zoomstep;
} }
} }
if (event.type == SDL_KEYDOWN) { if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) { switch (event.key.keysym.sym) {
case SDLK_LEFT: case SDLK_LEFT:
case SDLK_a: case SDLK_a:
Env::leftPressed = true; Env::leftPressed = true;
if (GameObjects::audioPlayer) { if (GameObjects::audioPlayer) {
GameObjects::audioPlayer->playSound("Звук-Идут-по-земле.ogg"); GameObjects::audioPlayer->playSound("Звук-Идут-по-земле.ogg");
} }
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
case SDLK_d: case SDLK_d:
Env::rightPressed = true; Env::rightPressed = true;
if (GameObjects::audioPlayer) { if (GameObjects::audioPlayer) {
GameObjects::audioPlayer->playSound("Звук-Идут-по-земле.ogg"); GameObjects::audioPlayer->playSound("Звук-Идут-по-земле.ogg");
} }
break; break;
case SDLK_UP: case SDLK_UP:
case SDLK_w: case SDLK_w:
Env::upPressed = true; Env::upPressed = true;
if (GameObjects::audioPlayer) { if (GameObjects::audioPlayer) {
GameObjects::audioPlayer->playSound("Звук-Идут-по-земле.ogg"); GameObjects::audioPlayer->playSound("Звук-Идут-по-земле.ogg");
} }
break; break;
case SDLK_DOWN: case SDLK_DOWN:
case SDLK_s: case SDLK_s:
Env::downPressed = true; Env::downPressed = true;
if (GameObjects::audioPlayer) { if (GameObjects::audioPlayer) {
GameObjects::audioPlayer->playSound("Звук-Идут-по-земле.ogg"); GameObjects::audioPlayer->playSound("Звук-Идут-по-земле.ogg");
} }
break; break;
case SDLK_SPACE: case SDLK_SPACE:
// Play the symphony when space is pressed // Play the symphony when space is pressed
if (GameObjects::audioPlayer) { if (GameObjects::audioPlayer) {
GameObjects::audioPlayer->playMusic("Symphony No.6 (1st movement).ogg"); GameObjects::audioPlayer->playMusic("Symphony No.6 (1st movement).ogg");
} }
break; break;
} // Добавляем тестовые клавиши
case SDLK_i: // Добавить предмет по нажатию I
{
static int testItemCount = 0;
auto testTexture = std::make_shared<Texture>(CreateTextureDataFromBmp24("./book03.bmp"));
AddItemToInventory("TestItem_" + std::to_string(testItemCount++), testTexture);
std::cout << "Added test item to inventory\n";
PrintInventory();
}
break;
case SDLK_o: // Удалить последний предмет по нажатию O
{
auto inventory = ReturnInventory();
if (!inventory.empty()) {
RemoveItemFromInventory(inventory.back().name);
std::cout << "Removed last item from inventory\n";
PrintInventory();
}
}
break;
case SDLK_p: // Вывести текущий инвентарь по нажатию P
std::cout << "\nCurrent inventory contents:\n";
PrintInventory();
break;
}
} }
if (event.type == SDL_KEYUP) { if (event.type == SDL_KEYUP) {
switch (event.key.keysym.sym) { switch (event.key.keysym.sym) {
case SDLK_LEFT: case SDLK_LEFT:
case SDLK_a: case SDLK_a:
Env::leftPressed = false; Env::leftPressed = false;
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
case SDLK_d: case SDLK_d:
Env::rightPressed = false; Env::rightPressed = false;
break; break;
case SDLK_UP: case SDLK_UP:
case SDLK_w: case SDLK_w:
Env::upPressed = false; Env::upPressed = false;
break; break;
case SDLK_DOWN: case SDLK_DOWN:
case SDLK_s: case SDLK_s:
Env::downPressed = false; Env::downPressed = false;
break; break;
} }
} }
} }
render(); render();
} }
}; };