- [NEW] SetViewport() i resetViewport()
This commit is contained in:
@@ -28,6 +28,8 @@ namespace draw
|
|||||||
|
|
||||||
surface *textsurf = nullptr; // Surface on guardar el gif amb la font
|
surface *textsurf = nullptr; // Surface on guardar el gif amb la font
|
||||||
|
|
||||||
|
SDL_Rect viewport;
|
||||||
|
|
||||||
// Inicialització de tot el que fa falta per a carregar gràfics i pintar en pantalla
|
// Inicialització de tot el que fa falta per a carregar gràfics i pintar en pantalla
|
||||||
void init(const std::string &titol, const uint16_t width, const uint16_t height, const int zoom)
|
void init(const std::string &titol, const uint16_t width, const uint16_t height, const int zoom)
|
||||||
{
|
{
|
||||||
@@ -150,6 +152,7 @@ namespace draw
|
|||||||
{
|
{
|
||||||
// Si han especificat nullptr, fiquem "screen" com a destinació
|
// Si han especificat nullptr, fiquem "screen" com a destinació
|
||||||
destination = surf == nullptr ? screen : surf;
|
destination = surf == nullptr ? screen : surf;
|
||||||
|
resetViewport();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Estableix una superficie com a superficie de la que s'agafaràn els gràfics per a pintar
|
// Estableix una superficie com a superficie de la que s'agafaràn els gràfics per a pintar
|
||||||
@@ -169,6 +172,21 @@ namespace draw
|
|||||||
source = pushedSource;
|
source = pushedSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setViewport(const int x, const int y, const int w, const int h)
|
||||||
|
{
|
||||||
|
viewport.x = x>0?x:0;
|
||||||
|
viewport.y = y>0?y:0;
|
||||||
|
viewport.w = w+x<destination->w?w:destination->w;
|
||||||
|
viewport.h = h+y<destination->h?h:destination->h;
|
||||||
|
}
|
||||||
|
|
||||||
|
void resetViewport()
|
||||||
|
{
|
||||||
|
viewport.x = viewport.y = 0;
|
||||||
|
viewport.w = destination->w;
|
||||||
|
viewport.h = destination->h;
|
||||||
|
}
|
||||||
|
|
||||||
// Estableix la paleta del sistema carregant-la d'un GIF
|
// Estableix la paleta del sistema carregant-la d'un GIF
|
||||||
void loadPalette(const std::string &filename)
|
void loadPalette(const std::string &filename)
|
||||||
{
|
{
|
||||||
@@ -210,10 +228,16 @@ namespace draw
|
|||||||
// Funció interna per a pintar un pixel d'una superficie sense eixir-se'n de la memòria i petar el mame
|
// Funció interna per a pintar un pixel d'una superficie sense eixir-se'n de la memòria i petar el mame
|
||||||
void pset(surface *surface, const int x, const int y, const uint8_t color)
|
void pset(surface *surface, const int x, const int y, const uint8_t color)
|
||||||
{
|
{
|
||||||
// Si la coordenada està dins del rang que abarca la superficie,
|
// Si el color es transparent, eixim, ni ens molestem en mirar res més
|
||||||
// escriure el byte "color" on toca en el array de pixels de la superficie
|
if (color == transparent) return;
|
||||||
if (color != transparent && x >= 0 && y >= 0 && x < surface->w && y < surface->h)
|
|
||||||
{
|
// Si pintem a "destination", mirem que estiga dins del "viewport" i sinó fora
|
||||||
|
if (surface == destination) {
|
||||||
|
if (x+viewport.x >= 0 && y+viewport.y >= 0 && x < viewport.w && y < viewport.h)
|
||||||
|
surface->pixels[(viewport.x+x) + (y+viewport.y) * surface->w] = color_indices[color];
|
||||||
|
} else {
|
||||||
|
// Si no es destinations, mirem que estiga dins de la surface, i sinó fora!
|
||||||
|
if (x >= 0 && y >= 0 && x < destination->w && y < destination->h)
|
||||||
surface->pixels[x + y * surface->w] = color_indices[color];
|
surface->pixels[x + y * surface->w] = color_indices[color];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,14 +245,16 @@ namespace draw
|
|||||||
// Funció interna per a llegir un pixel d'una superficie eixir-se'n de la memòria i petar el mame
|
// Funció interna per a llegir un pixel d'una superficie eixir-se'n de la memòria i petar el mame
|
||||||
const uint8_t pget(surface *surface, const int x, const int y)
|
const uint8_t pget(surface *surface, const int x, const int y)
|
||||||
{
|
{
|
||||||
// Si la coordenada està dins del rang que abarca la superficie,
|
// Si estem llegint de "destination", mirar que estigam llegint dins del viewport
|
||||||
// tornar el byte que toca del array de pixels de la superficie
|
if (surface == destination) {
|
||||||
if (x >= 0 && y >= 0 && x < surface->w && y < surface->h)
|
if (x+viewport.x >= 0 && y+viewport.y >= 0 && x < viewport.w && y < viewport.h)
|
||||||
{
|
return surface->pixels[(viewport.x + x) + (viewport.y + y) * surface->w];
|
||||||
|
} else {
|
||||||
|
// Si no es "destination", si la coordenada està dins del rang que abarca la superficie,
|
||||||
|
if (x >= 0 && y >= 0 && x < destination->w && y < destination->h)
|
||||||
return surface->pixels[x + y * surface->w];
|
return surface->pixels[x + y * surface->w];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si no, algo tenim que tornar... pos "0"
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ namespace draw
|
|||||||
void pushSource();
|
void pushSource();
|
||||||
void popSource();
|
void popSource();
|
||||||
|
|
||||||
|
void setViewport(const int x, const int y, const int w, const int h);
|
||||||
|
void resetViewport();
|
||||||
|
|
||||||
/// @brief Estableix la paleta del sistema carregant-la d'un GIF
|
/// @brief Estableix la paleta del sistema carregant-la d'un GIF
|
||||||
/// @param filename nom de l'arxiu GIF d'on carregar la paleta
|
/// @param filename nom de l'arxiu GIF d'on carregar la paleta
|
||||||
void loadPalette(const std::string &filename);
|
void loadPalette(const std::string &filename);
|
||||||
|
|||||||
Reference in New Issue
Block a user