Trabajando en el marcador

This commit is contained in:
2022-09-21 22:41:08 +02:00
parent 488e603ca5
commit bc0bd2a4e2
6 changed files with 124 additions and 19 deletions

View File

@@ -19,8 +19,8 @@
#define GAMECANVAS_HEIGHT 240
// Zona de juego
const int PLAY_AREA_TOP = (0 * BLOCK);
const int PLAY_AREA_BOTTOM = (26 * BLOCK);
const int PLAY_AREA_TOP = (4 * BLOCK);
const int PLAY_AREA_BOTTOM = (30 * BLOCK);
const int PLAY_AREA_LEFT = (0 * BLOCK);
const int PLAY_AREA_RIGHT = (40 * BLOCK);
const int PLAY_AREA_WIDTH = PLAY_AREA_RIGHT - PLAY_AREA_LEFT;

View File

@@ -12,6 +12,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D
this->debug->setEnabled(true);
// Reserva memoria para los objetos
scoreboard = new ScoreBoard(renderer, asset, &board);
eventHandler = new SDL_Event();
itemTracker = new ItemTracker();
map = new Map(asset->get("01.map"), renderer, asset, itemTracker);
@@ -27,12 +28,15 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D
section.subsection = SUBSECTION_GAME_PLAY;
musicEnabled = !debug->getEnabled();
;
board.diamonds = 0;
board.lives = 5;
}
// Destructor
Game::~Game()
{
delete scoreboard;
delete eventHandler;
delete itemTracker;
delete map;
@@ -79,6 +83,7 @@ void Game::update()
// Actualiza los objetos
debug->clear();
scoreboard->update();
map->update();
enemyEngine->update();
player->update();
@@ -99,6 +104,7 @@ void Game::render()
map->render();
enemyEngine->render();
player->render();
scoreboard->render();
renderDebugInfo();
// Actualiza la pantalla
@@ -212,7 +218,7 @@ void Game::renderDebugInfo()
SDL_RenderFillRect(renderer, &rect);
// Pinta el texto
debug->setPos({1, 1});
debug->setPos({1, PLAY_AREA_TOP});
debug->render();
}

View File

@@ -10,6 +10,7 @@
#include "item_tracker.h"
#include "enemy_engine.h"
#include "text.h"
#include "scoreboard.h"
#include "debug.h"
#ifndef GAME_H
@@ -28,6 +29,8 @@ private:
Player *player; // Objeto para gestionar el jugador
ItemTracker *itemTracker; // Objeto para gestionar los items recogidos
EnemyEngine *enemyEngine; // Objeto encargado de gestionar los enemigos
ScoreBoard *scoreboard; // Objeto encargado de gestionar el marcador
board_t board; // Estructura con los datos del marcador
Debug *debug; // Objeto para gestionar la información de debug
section_t section; // Seccion actual dentro del programa
int ticks; // Contador de ticks para ajustar la velocidad del programa

View File

@@ -407,11 +407,11 @@ void Map::fillMapTexture()
SDL_RenderClear(renderer);
// Dibuja el degradado de fondo
const int num_lines = 208;
const float num_lines = PLAY_AREA_BOTTOM - PLAY_AREA_TOP;
for (int i = 0; i < num_lines; i++)
for (int i = PLAY_AREA_TOP; i < PLAY_AREA_BOTTOM; i++)
{
float step = ((float)i / (float)num_lines);
float step = ((float)i / num_lines);
int r = bgColor1.r + ((bgColor2.r - bgColor1.r) * step);
int g = bgColor1.g + ((bgColor2.g - bgColor1.g) * step);
int b = bgColor1.b + ((bgColor2.b - bgColor1.b) * step);
@@ -428,16 +428,7 @@ void Map::fillMapTexture()
// con lo que esta pintando desde fuera de la textura
clip.x = ((tilemap_l0[(y * map_width) + x] - 1) % tileset_width) * tile_size;
clip.y = ((tilemap_l0[(y * map_width) + x] - 1) / tileset_width) * tile_size;
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
}
// Dibuja el degradado del marcador
int color = 105;
for (int i = 208; i < 240; i++)
{
SDL_SetRenderDrawColor(renderer, 0x69, color, 0x69, 0xFF);
SDL_RenderDrawLine(renderer, 0, i, 320, i);
color--;
texture_tile->render(renderer, x * tile_size, (y * tile_size) + PLAY_AREA_TOP, &clip);
}
}
@@ -457,7 +448,7 @@ void Map::fillMapTexture()
// con lo que esta pintando desde fuera de la textura
clip.x = ((tilemap_l1[(y * map_width) + x] - 1) % tileset_width) * tile_size;
clip.y = ((tilemap_l1[(y * map_width) + x] - 1) / tileset_width) * tile_size;
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
texture_tile->render(renderer, x * tile_size, (y * tile_size) + PLAY_AREA_TOP, &clip);
}
}
@@ -496,7 +487,7 @@ e_tile_map Map::getTile(SDL_Point p)
const int y = std::max(getPlayArea(b_top), (std::min(p.y, getPlayArea(b_bottom) - 1)));
// Calcula el tile
const int tile = collisionmap[((y / tile_size) * map_width) + (x / tile_size)];
const int tile = collisionmap[(((y + PLAY_AREA_TOP) / tile_size) * map_width) + (x / tile_size)];
// Las 8 primeras filas son tiles de fondo
if (tile == 0)

56
source/scoreboard.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include "scoreboard.h"
#include <fstream>
#include <sstream>
// Constructor
ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset, board_t *board)
{
// Obten punteros a objetos
this->asset = asset;
this->renderer = renderer;
this->board = board;
// Reserva memoria para los objetos
texture = new LTexture(renderer, asset->get("player.png"));
sprite = new Sprite({0, 0, 0, 0}, texture, renderer);
text = new Text(asset->get("dogica.png"), asset->get("dogica.txt"), renderer);
// Inicializa las variables
counter = 0;
}
// Destructor
ScoreBoard::~ScoreBoard()
{
delete texture;
delete sprite;
delete text;
}
// Pinta el objeto en pantalla
void ScoreBoard::render()
{
// Ponenegro el fondo del marcador
const SDL_Rect rect = {0, 0, PLAY_AREA_WIDTH, 2 * 8};
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
// Escribe los textos
text->writeCentered(PLAY_AREA_CENTER_FIRST_QUARTER_X, 1, "-LIVES-");
text->writeCentered(PLAY_AREA_CENTER_FIRST_QUARTER_X, 1 + text->getCharacterSize(), std::to_string(board->lives));
text->writeCentered(PLAY_AREA_CENTER_THIRD_QUARTER_X, 1, "-DIAMONDS-");
text->writeCentered(PLAY_AREA_CENTER_THIRD_QUARTER_X, 1 + text->getCharacterSize(), std::to_string(board->diamonds));
}
// Actualiza las variables del objeto
void ScoreBoard::update()
{
counter++;
}
// Recarga la textura
void ScoreBoard::reLoadTexture()
{
texture->reLoad();
text->reLoadTexture();
}

49
source/scoreboard.h Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
#include <SDL2/SDL.h>
#include "utils.h"
#include "text.h"
#include "asset.h"
#include "sprite.h"
#include "const.h"
#include <string>
#ifndef SCOREBOARD_H
#define SCOREBOARD_H
struct board_t
{
int diamonds; // Lleva la cuenta de los objetos recogidos
int lives; // Lleva la cuenta de ls vidas restantes del jugador
};
// Clase ScoreBoard
class ScoreBoard
{
private:
LTexture *texture; // Textura con los graficos
Sprite *sprite; // Sprite para mostrar los graficos
SDL_Renderer *renderer; // El renderizador de la ventana
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
Text *text; // Objeto para escribir texto
int counter; // Contador interno
board_t *board; // Contiene las variables a mostrar en el marcador
public:
// Constructor
ScoreBoard(SDL_Renderer *renderer, Asset *asset, board_t *board);
// Destructor
~ScoreBoard();
// Pinta el objeto en pantalla
void render();
// Actualiza las variables del objeto
void update();
// Recarga la textura
void reLoadTexture();
};
#endif