| 1 |
/*************************************************************************** |
| 2 |
graphic.cpp - description |
| 3 |
------------------- |
| 4 |
begin : Sat Oct 14 2000 |
| 5 |
copyright : (C) 2000 by Waldemar Baraldi |
| 6 |
email : [email protected] |
| 7 |
***************************************************************************/ |
| 8 |
|
| 9 |
/*************************************************************************** |
| 10 |
* * |
| 11 |
* This program is free software; you can redistribute it and/or modify * |
| 12 |
* it under the terms of the GNU General Public License as published by * |
| 13 |
* the Free Software Foundation; either version 2 of the License, or * |
| 14 |
* (at your option) any later version. * |
| 15 |
* * |
| 16 |
***************************************************************************/ |
| 17 |
|
| 18 |
#include "graphic.h" |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
Graphic::Graphic():Surface(){ |
| 22 |
pixels=NULL; |
| 23 |
clip_active=false; |
| 24 |
}; |
| 25 |
|
| 26 |
Graphic::Graphic(int width, int height) |
| 27 |
:Surface(width, height){ |
| 28 |
pixels=NULL; |
| 29 |
clip_active=false; |
| 30 |
} |
| 31 |
|
| 32 |
Graphic::Graphic(Graphic * g){ |
| 33 |
pixels= malloc(g->surface->w*g->surface->h*g->surface->format->BytesPerPixel); |
| 34 |
memcpy(pixels, g->surface->pixels,g->surface->w*g->surface->h*g->surface->format->BytesPerPixel); |
| 35 |
|
| 36 |
surface=SDL_CreateRGBSurfaceFrom(pixels, |
| 37 |
g->surface->w, g->surface->h, g->surface->format->BitsPerPixel, |
| 38 |
g->surface->pitch, g->surface->format->Rmask, g->surface->format->Gmask, |
| 39 |
g->surface->format->Bmask, g->surface->format->Amask); |
| 40 |
clip_active=false; |
| 41 |
} |
| 42 |
|
| 43 |
Graphic::~Graphic(){ |
| 44 |
if (pixels) free(pixels); |
| 45 |
} |
| 46 |
|
| 47 |
void Graphic::setAlpha(char value=OPAQUE){ |
| 48 |
SDL_SetAlpha(surface, SDL_SRCALPHA, value); |
| 49 |
} |
| 50 |
|
| 51 |
void Graphic::enableClipping(bool flag=true){ |
| 52 |
|
| 53 |
if (flag){ |
| 54 |
SDL_Rect rect; |
| 55 |
rect.x=clip_x; |
| 56 |
rect.y=clip_y; |
| 57 |
rect.w=clip_width; |
| 58 |
rect.h=clip_height; |
| 59 |
SDL_SetClipRect(surface, &rect); |
| 60 |
}else |
| 61 |
SDL_SetClipRect(surface, NULL); |
| 62 |
} |
| 63 |
|
| 64 |
void Graphic::setClipping(int x, int y, int width, int height){ |
| 65 |
clip_y=y; |
| 66 |
clip_x=x; |
| 67 |
clip_width=width; |
| 68 |
clip_height=height; |
| 69 |
} |
| 70 |
|
| 71 |
void Graphic::flip(Axis a=HAxis){ |
| 72 |
if (surface){ |
| 73 |
if (SDL_MUSTLOCK(surface)) |
| 74 |
if (SDL_LockSurface(surface)<0) return; |
| 75 |
int i,j,width=surface->w, height=surface->h, aw,bw; |
| 76 |
Uint32 * p, *q, aux, *ap; |
| 77 |
p=(Uint32*)(surface->pixels); |
| 78 |
if (a==HAxis){ |
| 79 |
q=p+width*height-1; |
| 80 |
height/=2; |
| 81 |
for (j=0;j<height;j++){ |
| 82 |
for (i=0;i<width;i++){ |
| 83 |
aux=*p; |
| 84 |
*p++=*q; |
| 85 |
*q--=aux; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
else{ |
| 90 |
aw=width>>1; |
| 91 |
bw=width-1; |
| 92 |
for (j=0;j<height;j++){ |
| 93 |
ap=p; |
| 94 |
q=p+bw; |
| 95 |
for (i=0;i<aw;i++){ |
| 96 |
aux=*p; |
| 97 |
*p++=*q; |
| 98 |
*q--=aux; |
| 99 |
} |
| 100 |
p=ap+width; |
| 101 |
} |
| 102 |
} |
| 103 |
if (SDL_MUSTLOCK(surface)) |
| 104 |
SDL_UnlockSurface(surface); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
void Graphic::fill(int r, int g, int b){ |
| 109 |
SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, r, g, b)); |
| 110 |
} |
| 111 |
|
| 112 |
void Graphic::fillRect(int x, int y, int w, int h, int r, int g, int b, int a){ |
| 113 |
SDL_Rect rect; |
| 114 |
|
| 115 |
rect.x=x; |
| 116 |
rect.y=y; |
| 117 |
rect.w=w; |
| 118 |
rect.h=h; |
| 119 |
|
| 120 |
SDL_FillRect(surface, &rect, SDL_MapRGBA(surface->format, r, g, b, a)); |
| 121 |
} |
| 122 |
|