2
* Copyright (C) 2001 by Holger Rapp
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28
* This functions is responsible for displaying graphics and keeping them up to date
30
* It's little strange in it's interface, but it is optimzed for speed, not beauty
34
/** Graphic::Graphic(void)
36
* Default Constructor. Simple Inits
41
Graphic::Graphic(void) {
49
SDL_Init(SDL_INIT_VIDEO);
52
/** Graphic::~Graphic(void)
59
Graphic::~Graphic(void) {
72
/** void Graphic::set_mode(const unsigned short x, const unsigned short y, const Mode m)
74
* This function sets a new graphics mode.
76
* Args: x x resolution
78
* m either windows or fullscreen
81
void Graphic::set_mode(const unsigned short x, const unsigned short y, const Mode m) {
86
sc = SDL_SetVideoMode(x, y, 16, SDL_SWSURFACE | SDL_FULLSCREEN);
89
sc = SDL_SetVideoMode(x, y, 16, SDL_SWSURFACE);
95
pixels=(unsigned short*) sc->pixels;
102
/** void Graphic::update_screen(void);
104
* This updates the whole screen
109
void Graphic::update_screen(void) {
115
SDL_UpdateRect(sc, 0, 0, xres, yres);
118
/** void Graphic::update_rect(const unsigned short x, const unsigned short y, const unsigned short w, const unsigned short h);
120
* This updates a rect of the screen
122
* Args: x upper left corner of rect
123
* y upper left corner of rect
127
void Graphic::update_rect(const unsigned short x, const unsigned short y, const unsigned short w, const unsigned short h) {
133
SDL_UpdateRect(sc, x, y, w, h);
136
/** void Graphic::update_quarter(void)
138
* This function updates a bit of the screen, but this bit moves around each time
139
* this function gets called.
140
* So it's made sure, that the whole screen gets updated, when this function gets called
146
void Graphic::update_quarter(void) {
147
static unsigned int x;
148
static unsigned int y;
149
static unsigned int xadd=xres/4;
150
static unsigned int yadd=yres/4;
152
if(x==xres) { x=0; y+=yadd; }
155
SDL_UpdateRect(sc, x, y, xadd, yadd);
159
/** void draw_pic(Pic* p, const unsigned short d_x_pos, const unsigned short d_y_pos, const unsigned short p_x_pos,
160
* const unsigned short p_y_pos, const unsigned short i_w, const unsigned short i_h)
162
* This functions plots a picture onto the current screen
164
* friend to class pic and class Graphic
166
* Args: p picture to plot
167
* d_x_pos xpos on screen
168
* d_y_pos ypos on screen
169
* p_x_pos start xpos in picture
170
* p_y_pos start ypos in picture
175
void draw_pic(Pic* p, const unsigned short d_x_pos, const unsigned short d_y_pos, const unsigned short p_x_pos, const unsigned short p_y_pos,
176
const unsigned short i_w, const unsigned short i_h) {
178
unsigned short w=i_w;
179
unsigned short h=i_h;
181
if(d_x_pos+w>g_gr.get_xres()) w=g_gr.get_xres()-d_x_pos;
182
if(d_y_pos+h>g_gr.get_yres()) h=g_gr.get_yres()-d_y_pos;
184
if(p->has_clrkey()) {
185
// Slow blit, checking for clrkeys. This could probably speed up by copying
186
// 2 pixels (==4bytes==register width)
187
// in one rush. But this is a nontrivial task
188
for(unsigned long y=0; y<h; y++) {
189
clr=p->get_pixel(p_x_pos, p_y_pos+y);
190
if(clr != p->get_clrkey()) g_gr.set_pixel(d_x_pos, d_y_pos+y, clr);
191
else g_gr.set_cpixel(d_x_pos, d_y_pos+y);
192
for(unsigned long x=1; x<w; x++) {
194
if(clr != p->get_clrkey()) g_gr.set_npixel(clr);
199
unsigned long poffs=p->get_w()*p_y_pos + p_x_pos;
200
unsigned long doffs=g_gr.get_xres()*d_y_pos + d_x_pos;
202
// fast blitting, using memcpy
203
for(unsigned long y=0; y<h; y++) {
204
memcpy(g_gr.pixels+doffs, p->pixels+poffs, w<<1); // w*sizeof(short)
205
doffs+=g_gr.get_xres();
211
/** void copy_pic(Pic* dst, Pic* src, const unsigned short d_x_pos, const unsigned short d_y_pos, const unsigned short p_x_pos,
212
* const unsigned short p_y_pos, const unsigned short i_w, const unsigned short i_h)
214
* This functions plots a picture into an other
216
* friend to class pic
218
* Args: src picture to plot
219
* dst picture to plot inside
220
* d_x_pos xpos on screen
221
* d_y_pos ypos on screen
222
* p_x_pos start xpos in picture
223
* p_y_pos start ypos in picture
228
void copy_pic(Pic* dst, Pic* src, const unsigned short d_x_pos, const unsigned short d_y_pos, const unsigned short p_x_pos,
229
const unsigned short p_y_pos, const unsigned short i_w, const unsigned short i_h) {
231
unsigned short w=i_w;
232
unsigned short h=i_h;
234
if(d_x_pos+w>g_gr.get_xres()) w=g_gr.get_xres()-d_x_pos;
235
if(d_y_pos+h>g_gr.get_yres()) h=g_gr.get_yres()-d_y_pos;
237
if(src->has_clrkey() && (dst->get_clrkey()!=src->get_clrkey())) {
238
for(unsigned long y=0; y<h; y++) {
239
clr=src->get_pixel(p_x_pos, p_y_pos+y);
240
dst->set_pixel(d_x_pos, d_y_pos+y, clr);
241
for(unsigned long x=1; x<w; x++) {
242
clr=src->get_npixel();
243
dst->set_npixel(clr);
247
unsigned long soffs=src->get_w()*p_y_pos + p_x_pos;
248
unsigned long doffs=dst->get_w()*d_y_pos + d_x_pos;
250
// fast blitting, using memcpy
251
for(unsigned long y=0; y<h; y++) {
252
memcpy(dst->pixels+doffs, src->pixels+soffs, w<<1); // w*sizeof(short)