-[FIX] Hard to catch memory bug has been valgrinded out!
This commit is contained in:
@@ -47,7 +47,7 @@ namespace draw
|
|||||||
|
|
||||||
// Creem la superficie "screen" i la establim com a superficie destinació
|
// Creem la superficie "screen" i la establim com a superficie destinació
|
||||||
screen = createSurface(width, height);
|
screen = createSurface(width, height);
|
||||||
destination = screen;
|
setDestination(screen);
|
||||||
sel_color = transparent = 0;
|
sel_color = transparent = 0;
|
||||||
for (int i=0;i<256;++i) color_indices[i] = i;
|
for (int i=0;i<256;++i) color_indices[i] = i;
|
||||||
|
|
||||||
@@ -174,10 +174,10 @@ namespace draw
|
|||||||
|
|
||||||
void setViewport(const int x, const int y, const int w, const int h)
|
void setViewport(const int x, const int y, const int w, const int h)
|
||||||
{
|
{
|
||||||
viewport.x = x>0?x:0;
|
viewport.x = x<0 ? 0 : x>=destination->w ? 0 : x;
|
||||||
viewport.y = y>0?y:0;
|
viewport.y = y<0 ? 0 : y>=destination->h ? 0 : y;
|
||||||
viewport.w = w+x<destination->w?w:destination->w;
|
viewport.w = w+viewport.x<destination->w ? w : destination->w-viewport.x;
|
||||||
viewport.h = h+y<destination->h?h:destination->h;
|
viewport.h = h+viewport.y<destination->h ? h : destination->h-viewport.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void resetViewport()
|
void resetViewport()
|
||||||
@@ -241,28 +241,34 @@ namespace draw
|
|||||||
// Si el color es transparent, eixim, ni ens molestem en mirar res més
|
// Si el color es transparent, eixim, ni ens molestem en mirar res més
|
||||||
if (color == transparent) return;
|
if (color == transparent) return;
|
||||||
|
|
||||||
|
// Si està fora de la surface, directament passem
|
||||||
|
if (x < 0 || y < 0 || x >= surface->w || y >= surface->h)
|
||||||
|
return;
|
||||||
|
|
||||||
// Si pintem a "destination", mirem que estiga dins del "viewport" i sinó fora
|
// Si pintem a "destination", mirem que estiga dins del "viewport" i sinó fora
|
||||||
if (surface == destination) {
|
if (surface == destination) {
|
||||||
if (x+viewport.x >= 0 && y+viewport.y >= 0 && x < viewport.w && y < viewport.h)
|
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];
|
surface->pixels[(viewport.x+x) + (y+viewport.y) * surface->w] = color_indices[color];
|
||||||
} else {
|
} else {
|
||||||
// Si no es destinations, mirem que estiga dins de la surface, i sinó fora!
|
// Si no es destinations, pintem i au
|
||||||
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];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 està fora de la surface, directament passem
|
||||||
|
if (x < 0 || y < 0 || x >= surface->w || y >= surface->h)
|
||||||
|
return 0;
|
||||||
|
|
||||||
// Si estem llegint de "destination", mirar que estigam llegint dins del viewport
|
// Si estem llegint de "destination", mirar que estigam llegint dins del viewport
|
||||||
if (surface == destination) {
|
if (surface == destination) {
|
||||||
if (x+viewport.x >= 0 && y+viewport.y >= 0 && x < viewport.w && y < viewport.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];
|
return surface->pixels[(viewport.x + x) + (viewport.y + y) * surface->w];
|
||||||
} else {
|
} else {
|
||||||
// Si no es "destination", si la coordenada està dins del rang que abarca la superficie,
|
// Si no es "destination",
|
||||||
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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -380,12 +386,13 @@ namespace draw
|
|||||||
// Refresca la pantalla
|
// Refresca la pantalla
|
||||||
void render()
|
void render()
|
||||||
{
|
{
|
||||||
|
|
||||||
Uint32 *sdl_pixels; // Punter al array de pixels que enstornarà SDL_LockTexture
|
Uint32 *sdl_pixels; // Punter al array de pixels que enstornarà SDL_LockTexture
|
||||||
int sdl_pitch; // Ací estarà guardat el pitch de la textura, com es de 32 bits, no m'afecta
|
int sdl_pitch; // Ací estarà guardat el pitch de la textura, com es de 32 bits, no m'afecta
|
||||||
const uint32_t size = screen->w * screen->h; // tamany de la superficie
|
const uint32_t size = screen->w * screen->h; // tamany de la superficie
|
||||||
|
|
||||||
// Bloquejem la textura SDL i agafem els seus pixels (son enters de 32 bits amb format 0xAARRGGBB)
|
// Bloquejem la textura SDL i agafem els seus pixels (son enters de 32 bits amb format 0xAARRGGBB)
|
||||||
SDL_LockTexture(sdl_texture, NULL, (void **)&sdl_pixels, &sdl_pitch);
|
int result = SDL_LockTexture(sdl_texture, NULL, (void **)&sdl_pixels, &sdl_pitch);
|
||||||
|
|
||||||
// Cada pixel de la superficie "screen" es un enter de 8 bits que representa un index en la paleta de colors
|
// Cada pixel de la superficie "screen" es un enter de 8 bits que representa un index en la paleta de colors
|
||||||
// Per tant, per a pintar en la textura SDL, pillem el color de la paleta que correspon al index en "screen"
|
// Per tant, per a pintar en la textura SDL, pillem el color de la paleta que correspon al index en "screen"
|
||||||
|
|||||||
@@ -337,7 +337,7 @@ bool game::loop()
|
|||||||
draw::color(WHITE);
|
draw::color(WHITE);
|
||||||
draw::fillrect(0, 0, 100, 240);
|
draw::fillrect(0, 0, 100, 240);
|
||||||
|
|
||||||
draw::setViewport(420, 2, 100, 240);
|
draw::setViewport(420, 2, 100, 238);
|
||||||
|
|
||||||
draw::color(LIGHT+WHITE);
|
draw::color(LIGHT+WHITE);
|
||||||
draw::fillrect(2, 0, 96, 100);
|
draw::fillrect(2, 0, 96, 100);
|
||||||
@@ -369,7 +369,7 @@ bool game::loop()
|
|||||||
act = actor::getSelected();
|
act = actor::getSelected();
|
||||||
if (act)
|
if (act)
|
||||||
{
|
{
|
||||||
draw::setViewport(420, 90, 100, 240);
|
draw::setViewport(420, 90, 100, 150);
|
||||||
|
|
||||||
btn_txt("NAME:", 2, 0, act->name);
|
btn_txt("NAME:", 2, 0, act->name);
|
||||||
if (btn_opt2("BMP:", 2, 10, act->bmp, gifs)) {
|
if (btn_opt2("BMP:", 2, 10, act->bmp, gifs)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user