- 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 "systempalette.h"
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
{
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 <string>
#include <iostream>
#include "destsurface.h"
#include "systempalette.h"
// Constructor
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};
// 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 (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
setVideoMode(options->videoMode);
@@ -53,26 +57,34 @@ Screen::~Screen()
// Limpia la pantalla
void Screen::clean(color_t color)
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
SDL_RenderClear(renderer);
DestSurface::clear((color.r<<24) + (color.g<<16) + (color.b<<8) + 255);
}
// Prepara para empezar a dibujar en la textura de juego
void Screen::start()
{
SDL_SetRenderTarget(renderer, gameCanvas);
//SDL_SetRenderTarget(renderer, gameCanvas);
}
// Vuelca el contenido del renderizador en pantalla
void Screen::blit()
{
// Vuelve a dejar el renderizador en modo normal
SDL_SetRenderTarget(renderer, nullptr);
//SDL_SetRenderTarget(renderer, nullptr);
// Borra el contenido previo
SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
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
SDL_RenderCopy(renderer, gameCanvas, nullptr, &dest);

View File

@@ -4,13 +4,13 @@
#include "stb_image.h"
#include <iostream>
#include "systempalette.h"
#include "destsurface.h"
// 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
this->renderer = renderer;
this->texture = texture;
this->path = path;
// Inicializa
@@ -56,6 +56,8 @@ bool Texture::loadFromFile(std::string path, bool verbose)
// Limpia
unload();
this->width = width;
this->height = height;
const int size = width*height;
this->pixels = new uint8_t[size];
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
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
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;
// 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
void Texture::setAsRenderTarget(SDL_Renderer *renderer)
{
SDL_SetRenderTarget(renderer, texture);
//SDL_SetRenderTarget(renderer, texture);
}
// Obtiene el ancho de la imagen
@@ -154,5 +167,5 @@ bool Texture::reLoad()
// Obtiene la textura
SDL_Texture *Texture::getSDLTexture()
{
return texture;
return nullptr; //texture;
}

View File

@@ -12,7 +12,6 @@ class Texture
private:
// Objetos y punteros
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
// Variables
@@ -23,7 +22,7 @@ private:
public:
// 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
~Texture();
@@ -32,7 +31,7 @@ public:
bool loadFromFile(std::string path, bool verbose = false);
// 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
void unload();