- Canvis que vaig fer fa 2 messos, però no sé en què estava pensant, he de arreglar-ho.

This commit is contained in:
2023-02-13 11:50:37 +01:00
parent d230259e42
commit 908ffa6d55
5 changed files with 62 additions and 22 deletions

View File

@@ -1,16 +1,30 @@
#include "destsurface.h" #include "destsurface.h"
#include "systempalette.h"
namespace DestSurface namespace DestSurface
{ {
void Init(SDL_Renderer *renderer, SDL_Texture *texture) { uint8_t *pixels = nullptr;
int width, height;
void init(int width, int height)
{
if (pixels==nullptr) pixels = new uint8_t[width*height];
DestSurface::width = width;
DestSurface::height = height;
} }
void Clear() { void clear(uint32_t color)
{
SDL_memset(pixels, SystemPalette::getEntry(color), width*height);
} }
uint32_t *getPixels() { uint8_t *getPixels()
{
return pixels;
}
int getWidth()
{
return width;
} }
} }

View File

@@ -4,9 +4,11 @@
namespace DestSurface namespace DestSurface
{ {
void Init(SDL_Renderer *renderer, SDL_Texture *texture); void init(int width, int height);
void Clear(); void clear(uint32_t color);
uint32_t *getPixels(); uint8_t *getPixels();
int getWidth();
} }

View File

@@ -1,6 +1,8 @@
#include "screen.h" #include "screen.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
#include "destsurface.h"
#include "systempalette.h"
// Constructor // Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options) Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options)
@@ -28,7 +30,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
borderColor = {0x00, 0x00, 0x00}; borderColor = {0x00, 0x00, 0x00};
// Crea la textura donde se dibujan los graficos del juego // Crea la textura donde se dibujan los graficos del juego
gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight); gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, gameCanvasWidth, gameCanvasHeight);
if (gameCanvas == nullptr) if (gameCanvas == nullptr)
{ {
if (options->console) if (options->console)
@@ -37,6 +39,8 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
} }
} }
DestSurface::init(gameCanvasWidth, gameCanvasHeight);
// Establece el modo de video // Establece el modo de video
setVideoMode(options->videoMode); setVideoMode(options->videoMode);
@@ -53,26 +57,34 @@ Screen::~Screen()
// Limpia la pantalla // Limpia la pantalla
void Screen::clean(color_t color) void Screen::clean(color_t color)
{ {
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF); DestSurface::clear((color.r<<24) + (color.g<<16) + (color.b<<8) + 255);
SDL_RenderClear(renderer);
} }
// Prepara para empezar a dibujar en la textura de juego // Prepara para empezar a dibujar en la textura de juego
void Screen::start() void Screen::start()
{ {
SDL_SetRenderTarget(renderer, gameCanvas); //SDL_SetRenderTarget(renderer, gameCanvas);
} }
// Vuelca el contenido del renderizador en pantalla // Vuelca el contenido del renderizador en pantalla
void Screen::blit() void Screen::blit()
{ {
// Vuelve a dejar el renderizador en modo normal // Vuelve a dejar el renderizador en modo normal
SDL_SetRenderTarget(renderer, nullptr); //SDL_SetRenderTarget(renderer, nullptr);
// Borra el contenido previo // Borra el contenido previo
SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF); SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
uint8_t *destSurface = DestSurface::getPixels();
Uint32 *pixels;
int pitch;
int size = gameCanvasWidth*gameCanvasHeight;
SDL_LockTexture(gameCanvas, NULL, (void**)&pixels, &pitch);
for (int i=0;i<size;++i) pixels[i] = SystemPalette::getRGBA(destSurface[i]);
SDL_UnlockTexture(gameCanvas);
// Copia la textura de juego en el renderizador en la posición adecuada // Copia la textura de juego en el renderizador en la posición adecuada
SDL_RenderCopy(renderer, gameCanvas, nullptr, &dest); SDL_RenderCopy(renderer, gameCanvas, nullptr, &dest);

View File

@@ -4,13 +4,13 @@
#include "stb_image.h" #include "stb_image.h"
#include <iostream> #include <iostream>
#include "systempalette.h" #include "systempalette.h"
#include "destsurface.h"
// Constructor // Constructor
Texture::Texture(SDL_Renderer *renderer, SDL_Texture *texture, std::string path, bool verbose) Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose)
{ {
// Copia punteros // Copia punteros
this->renderer = renderer; this->renderer = renderer;
this->texture = texture;
this->path = path; this->path = path;
// Inicializa // Inicializa
@@ -56,6 +56,8 @@ bool Texture::loadFromFile(std::string path, bool verbose)
// Limpia // Limpia
unload(); unload();
this->width = width;
this->height = height;
const int size = width*height; const int size = width*height;
this->pixels = new uint8_t[size]; this->pixels = new uint8_t[size];
for (int i = 0; i < size; ++i) this->pixels[i] = SystemPalette::getEntry(data[i]); for (int i = 0; i < size; ++i) this->pixels[i] = SystemPalette::getEntry(data[i]);
@@ -66,7 +68,7 @@ bool Texture::loadFromFile(std::string path, bool verbose)
} }
// Crea una textura en blanco // Crea una textura en blanco
bool Texture::createBlank(int width, int height, SDL_TextureAccess access) bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access)
{ {
// Crea una textura sin inicializar // Crea una textura sin inicializar
this->pixels = new uint8_t[width*height]; this->pixels = new uint8_t[width*height];
@@ -124,13 +126,24 @@ void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float
renderQuad.h = renderQuad.h * zoomH; renderQuad.h = renderQuad.h * zoomH;
// Renderiza a pantalla // Renderiza a pantalla
SDL_RenderCopyEx(renderer, texture, clip, &renderQuad, angle, center, flip); //SDL_RenderCopyEx(renderer, texture, clip, &renderQuad, angle, center, flip);
uint8_t *destSurface = DestSurface::getPixels();
int canvasWidth = DestSurface::getWidth();
int y_inc = 1, y_ini = 0, y_fin = renderQuad.h-1; if (flip && SDL_FLIP_VERTICAL==SDL_FLIP_VERTICAL) { auto tmp=y_ini; y_ini=y_fin; y_fin=tmp; y_inc=-1; }
int x_inc = 1, x_ini = 0, x_fin = renderQuad.w-1; if (flip && SDL_FLIP_HORIZONTAL==SDL_FLIP_HORIZONTAL) { auto tmp=x_ini; x_ini=x_fin; x_fin=tmp; x_inc=-1; }
for (int yy=y_ini; yy <= y_fin; yy += y_inc) {
for (int xx=x_ini; xx <= x_fin; xx += x_inc) {
destSurface[ x+xx + y+yy * canvasWidth ] = pixels[ clip->x+xx + clip->y+yy * width ];
}
}
} }
// Establece la textura como objetivo de renderizado // Establece la textura como objetivo de renderizado
void Texture::setAsRenderTarget(SDL_Renderer *renderer) void Texture::setAsRenderTarget(SDL_Renderer *renderer)
{ {
SDL_SetRenderTarget(renderer, texture); //SDL_SetRenderTarget(renderer, texture);
} }
// Obtiene el ancho de la imagen // Obtiene el ancho de la imagen
@@ -154,5 +167,5 @@ bool Texture::reLoad()
// Obtiene la textura // Obtiene la textura
SDL_Texture *Texture::getSDLTexture() SDL_Texture *Texture::getSDLTexture()
{ {
return texture; return nullptr; //texture;
} }

View File

@@ -12,7 +12,6 @@ class Texture
private: private:
// Objetos y punteros // Objetos y punteros
uint8_t *pixels; // Los pixels de esta textura uint8_t *pixels; // Los pixels de esta textura
SDL_Texture *texture; // La textura SDL sobre la que pintaremos (debe haber sido creada con SDL_TEXTUREACCESS_STREAMING)
SDL_Renderer *renderer; // Renderizador donde dibujar la textura SDL_Renderer *renderer; // Renderizador donde dibujar la textura
// Variables // Variables
@@ -23,7 +22,7 @@ private:
public: public:
// Constructor // Constructor
Texture(SDL_Renderer *renderer, SDL_Texture *texture, std::string path = "", bool verbose = false); Texture(SDL_Renderer *renderer, std::string path = "", bool verbose = false);
// Destructor // Destructor
~Texture(); ~Texture();
@@ -32,7 +31,7 @@ public:
bool loadFromFile(std::string path, bool verbose = false); bool loadFromFile(std::string path, bool verbose = false);
// Crea una textura en blanco // Crea una textura en blanco
bool createBlank(int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING); bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);
// Libera la memoria de la textura // Libera la memoria de la textura
void unload(); void unload();