//Copyright 2010 Alvaro Diaz Sanchez
/*This program is free software: you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see
*/
/**
* @file graficos.cpp
* @brief Fichero con definiciones para la gestión de gráficos
*
* Implementa la interfaz de graficos.h
*
*/
/* ***************************************** */
#include
#include
#include "graficos.h"
/* ***************************************** */
using namespace std;
namespace {
// Variable que representa la pantalla (ventana) donde vamos a dibujar
SDL_Surface *ventana;
}
/* ***************************************** */
void CrearVentana(int ancho, int alto, const char *titulo)
{
// Iniciamos el módulo de gráficos de la SDL
if (SDL_Init(SDL_INIT_VIDEO)<0) {
cerr << "No se puede iniciar SDL ("<format, 0, 0, 0);
pixels = (Uint8 *)ventana->pixels;
for ( i=0; ih; ++i ) {
memset(pixels, black,
ventana->w*ventana->format->BytesPerPixel);
pixels += ventana->pitch;
}
SDL_UnlockSurface(ventana);
}
}
/* ***************************************** */
int FilasVentana()
{
return ventana->h;
}
/* ***************************************** */
int ColumnasVentana()
{
return ventana->w;
}
/* ***************************************** */
void Punto(int x, int y, unsigned char r, unsigned char g, unsigned char b)
{
pixelRGBA(ventana, x, y, r, g, b, 255);
SDL_UpdateRect(ventana, x, y, 1, 1);
}
/* ***************************************** */
void Linea(int x1, int y1, int x2, int y2,
unsigned char r, unsigned char g, unsigned char b)
{
lineRGBA(ventana, x1, y1, x2, y2, r, g, b, 255);
SDL_UpdateRect(ventana, min(x1,x2), min(y1,y2), abs(x1-x2)+1, abs(y1-y2)+1);
}
/* ***************************************** */
void Circulo(int x, int y, int rad,
unsigned char r, unsigned char g, unsigned char b)
{
circleRGBA(ventana, x, y, rad, r, g, b,255);
SDL_UpdateRect(ventana, x-rad, y-rad, rad*2+1, rad*2+1);
}
/* ***************************************** */
void CirculoR(int x, int y, int rad,
unsigned char r, unsigned char g, unsigned char b)
{
filledCircleRGBA(ventana, x, y, rad, r, g, b,255);
SDL_UpdateRect(ventana, x-rad, y-rad, rad*2+1, rad*2+1);
}
/* ***************************************** */
void Rectangulo(int x1, int y1, int x2, int y2,
unsigned char r, unsigned char g, unsigned char b)
{
rectangleRGBA(ventana, x1, y1, x2, y2, r, g, b, 255);
SDL_UpdateRect(ventana, min(x1,x2), min(y1,y2), abs(x1-x2)+1, abs(y1-y2)+1);
}
/* ***************************************** */
void RectanguloR(int x1, int y1, int x2, int y2,
unsigned char r, unsigned char g, unsigned char b)
{
boxRGBA(ventana, x1, y1, x2, y2, r, g, b, 255);
SDL_UpdateRect(ventana, min(x1,x2), min(y1,y2), abs(x1-x2)+1, abs(y1-y2)+1);
}
/* ***************************************** */
void Elipse(int x, int y, int radx, int rady,
unsigned char r, unsigned char g, unsigned char b)
{
ellipseRGBA(ventana, x, y, radx, rady, r, g, b,255);
SDL_UpdateRect(ventana, x-radx, y-rady, radx*2+1, rady*2+1);
}
/* ***************************************** */
void ElipseR(int x, int y, int radx, int rady,
unsigned char r, unsigned char g, unsigned char b)
{
filledEllipseRGBA(ventana, x, y, radx, rady, r, g, b,255);
SDL_UpdateRect(ventana, x-radx, y-rady, radx*2+1, rady*2+1);
}
/* ***************************************** */
void Texto(int x, int y, const char *c,
unsigned char r, unsigned char g, unsigned char b)
{
stringRGBA(ventana, x, y, c, r, g, b, 255);
SDL_UpdateRect(ventana, 0, 0, 0, 0);
}
/* ***************************************** */
// Obtenemos el color del punto que hay en las coordenadas (x,y)
// Devolvemos el valor en (r,g,b)
void ObtenerPunto(int x, int y,
unsigned char &r, unsigned char &g, unsigned char &b)
{
int bpp = ventana->format->BytesPerPixel;
Uint8 *p = (Uint8*)ventana->pixels + y*ventana->pitch + x*bpp;
Uint32 color=0;
// Bloqueamos la ventana si es necesario
if (SDL_MUSTLOCK(ventana)) {
if (SDL_LockSurface(ventana)<0) {
cerr << "Error al bloquear la ventana ("<format,&r,&g,&b);
}
/* ***************************************** */
SDL_Surface *LeerImagenBMP(const char *fich)
{
SDL_Surface *img;
if (!(img=SDL_LoadBMP(fich))) {
cerr << "No es posible cargar el fichero "<w;
y= img->h;
LiberarImagenBMP(img);
}
}
/* ***************************************** */
void DibujarImagenBMP(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
dest.w = img->w;
dest.h = img->h;
//SDL_SetAlpha(img, SDL_SRCALPHA, SDL_ALPHA_OPAQUE);
//SDL_SetColorKey(img, SDL_SRCCOLORKEY, SDL_MapRGB(img->format, 255, 255, 255));
SDL_BlitSurface(img, NULL, ventana, &dest);
SDL_UpdateRect(ventana, x, y, img->w, img->h);
}
/* ***************************************** */
int ObtenerClick(int &x, int &y)
{
SDL_Event evento;
int boton;
bool fin=false;
while (!fin) {
if (SDL_PollEvent(&evento)) {
switch (evento.type) {
case SDL_MOUSEBUTTONDOWN: // Pulsamos botón del ratón
boton = (int)(evento.button.button);
x = evento.button.x;
y = evento.button.y;
fin = true;
break;
case SDL_QUIT:
exit(1); //Terminación directa
}
}
}
return boton;
}
/* ***************************************** */
int ObtenerTecla()
{
SDL_Event evento;
int tecla;
bool fin=false;
while (!fin) {
if (SDL_PollEvent(&evento)) {
switch (evento.type) {
case SDL_KEYDOWN: // Pulsamos una tecla
tecla = (int)(evento.key.keysym.scancode);
fin = true;
break;
case SDL_QUIT:
exit(1); //Terminación directa
}
}
}
return tecla;
}
/* ***************************************** */
void Esperar(int ms)
{
SDL_Delay(ms);
}
/* ***************************************** */