#ifndef Graphics_H
#define Graphics_H
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include "types.h"
class Graphics
{
public:
// Constructor - Destructor de la clase
Graphics(SDL_Surface * screen);
~Graphics();
// Métodos de la clase
void PrintScreen();
void PrintBoard(int board[62][38]);
void DrawText(char* string, TTF_Font * font, int x, int y, int fR, int fG, int fB);
void DrawTextInt(int number, TTF_Font * font, int x, int y, int fR, int fG, int fB);
void DrawSprite(SDL_Surface* imageSurface, int srcX, int srcY, int dstX, int dstY, int width, int height);
void DrawRectangle(int x, int y, int width, int height, unsigned int R, unsigned int G, unsigned int B);
TTF_Font * LoadFont(char * font, unsigned int size);
// Métodos DEBUG
void DebugString(char * string, int x, int y);
void DebugInt(int value, int x, int y);
// Propiedades de la clase
SDL_Surface * screen;
protected:
private:
TTF_Font * fontDebug;
SDL_Surface * background;
};
#endif
#include "Graphics.h"
// Constructor de la clase
Graphics::Graphics(SDL_Surface * screen)
{
this->screen = screen;
TTF_Init();
fontDebug = LoadFont("./arial.ttf", 14);
background = IMG_Load("sdltest.png"); // <--- aki peta!!!
}
Graphics::~Graphics()
{
TTF_CloseFont(fontDebug);
TTF_Quit();
SDL_FreeSurface(screen);
// SDL_FreeSurface(background);
}
void Graphics::PrintScreen()
{
// Pintamos la pantalla de color Negro
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0,0,0));
// Pintamos la imagen de fondo
DrawSprite(background, 4, 4, 0, 0, 310, 190);
// Pintamos pantalla en negro
DrawRectangle(0, 0, 320, 240, 0, 0, 0);
// Pintamos pantalla juego
DrawRectangle(0, 0, 320, 200, 0, 0, 128);
DrawRectangle(4, 4, 310, 190, 0, 0, 255);
// Pintamos zona marcadores
DrawRectangle(0, 199, 320, 40, 0, 128, 255);
// DrawRectangle(101, 210, 90, 20, 128, 0, 255);
// DrawRectangle(261, 210, 55, 20, 128, 0, 255);
// Pintamos Marcadores
DrawText("TIME", fontDebug, 20, 200, 255, 0, 0);
DrawText("00020 sec.", fontDebug, 10, 215, 255, 255, 255);
DrawText("SCORE", fontDebug, 94, 200, 255, 0, 0);
DrawText("0000000", fontDebug, 90, 215, 255, 255, 255);
}
void Graphics::PrintBoard(int board[62][38])
{
// Pintamos tablero
for(int i=0;i<62;i++)
{
for(int j=0;j<38;j++)
{
switch(board[i][j])
{
case PLAYER:
DrawRectangle(4+i*5, 4+j*5, 5, 5, 255, 0, 0);
break;
case PATH_PLAYER:
DrawRectangle(4+i*5, 4+j*5, 5, 5, 255, 90, 0);
break;
case CPU:
DrawRectangle(4+i*5, 4+j*5, 5, 5, 255, 229, 5);
break;
case PATH_CPU:
DrawRectangle(4+i*5, 4+j*5, 5, 5, 255, 229, 70);
break;
default:
DrawRectangle(4+i*5, 4+j*5, 5, 5,0, 0, 255);
break;
}
}
}
// Pintamos zona marcadores
DrawRectangle(0, 199, 320, 40, 0, 128, 255);
DrawRectangle(101, 210, 90, 20, 128, 0, 255);
DrawRectangle(261, 210, 55, 20, 128, 0, 255);
TTF_Font * font = LoadFont("./arial.ttf", 12);
DrawText("Tiempo en juego: 9999 segundos", font, 5, 210, 255, 255, 255);
DrawText("Puntos: 99999999", font, 220, 210, 255, 255, 255);
TTF_CloseFont(font);
}
// Cargamos fuente
TTF_Font * Graphics::LoadFont(char * font, unsigned int size)
{
return TTF_OpenFont(font, size);
}
// Pintamos texto en pantalla
void Graphics::DrawText(char * string, TTF_Font * font, int x, int y, int fR, int fG, int fB)
{
SDL_Color foregroundColor = { fR, fG, fB };
SDL_Surface * textSurface = TTF_RenderText_Blended(font, string, foregroundColor);
SDL_Rect textLocation = { x, y, 0, 0 };
SDL_BlitSurface(textSurface, NULL, screen, &textLocation);
SDL_FreeSurface(textSurface);
}
// Imprimimos texto por pantalla
void Graphics::DrawTextInt(int number, TTF_Font * font, int x, int y, int fR, int fG, int fB)
{
char tmp_num[16];
sprintf((char*)tmp_num, "%d", number);
SDL_Color foregroundColor = { fR, fG, fB };
SDL_Surface* textSurface = TTF_RenderText_Blended(font,tmp_num, foregroundColor);
SDL_Rect textLocation = { x, y, 0, 0 };
SDL_BlitSurface(textSurface, NULL, screen, &textLocation);
SDL_FreeSurface(textSurface);
}
// Pintamos sprite
void Graphics::DrawSprite(SDL_Surface* imageSurface,int srcX, int srcY, int dstX, int dstY, int width, int height)
{
SDL_Rect srcRect;
srcRect.x = srcX;
srcRect.y = srcY;
srcRect.w = width;
srcRect.h = height;
SDL_Rect dstRect;
dstRect.x = dstX;
dstRect.y = dstY;
dstRect.w = width;
dstRect.h = height;
SDL_BlitSurface(imageSurface, &srcRect, screen, &dstRect);
}
// Para mostrar mensajes por pantalla. Modo DEBUG
void Graphics::DebugString(char * string, int x, int y)
{
DrawText(string, fontDebug, x, y, 255, 255, 255);
}
void Graphics::DebugInt(int value, int x, int y)
{
DrawTextInt(value, fontDebug, x, y, 255, 255, 255);
}
// Pintamos un rectangulo en pantalla
void Graphics::DrawRectangle(int x, int y, int width, int height, unsigned int R, unsigned int G, unsigned int B)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = width;
rect.h = height;
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, R, G, B));
}
Powered by vBulletin® Version 4.2.5 Copyright © 2026 vBulletin Solutions Inc. All rights reserved.