~son-of-yhwh/widelands/experimental

« back to all changes in this revision

Viewing changes to src/pic.cc

  • Committer: sirver
  • Date: 2002-02-05 20:54:08 UTC
  • Revision ID: git-v1:df384fd3a5e53be1f37803d1c0381fa993844bf5
Initial revision


git-svn-id: https://widelands.svn.sourceforge.net/svnroot/widelands/trunk@2 37b2a8de-5219-0410-9f54-a31bc463ab9c

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2001 by Holger Rapp 
 
3
 * 
 
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.
 
8
 * 
 
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.
 
13
 * 
 
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.
 
17
 *
 
18
 */
 
19
 
 
20
#include "graphic.h"
 
21
 
 
22
namespace Graph {
 
23
                  /** Pic::Pic(const Pic& p)
 
24
                        *
 
25
                        * Copy constructor. Slow and simple
 
26
                        *
 
27
                        * Args: p       pic to copy
 
28
                        * Returns: Nothing
 
29
                        */
 
30
                  Pic::Pic(const Pic& p) {
 
31
                                         *this=p;
 
32
                  }
 
33
 
 
34
 
 
35
                  /** Pic& Pic::operator=(const Pic& p) 
 
36
                        *
 
37
                        * Copy operator. Simple and slow. Don't use often
 
38
                        *
 
39
                        * Args: p       pic to copy
 
40
                        * returns: *this
 
41
                        */
 
42
                  Pic& Pic::operator=(const Pic& p) {
 
43
                                         clrkey=p.clrkey;
 
44
                                         sh_clrkey=p.sh_clrkey;
 
45
                                         lpix=0;
 
46
                                         bhas_clrkey=p.bhas_clrkey;
 
47
 
 
48
                                         set_size(p.w, p.h);
 
49
                                         
 
50
                                         memcpy(pixels, p.pixels, sizeof(short)*w*h);
 
51
                                         
 
52
                                         return *this;
 
53
                  }
 
54
 
 
55
                  /** void Pic::set_size(const unsigned short nw, const unsigned short nh) 
 
56
                        *
 
57
                        * This functions sets the new size of a pic
 
58
                        *
 
59
                        * Args: nw      = new width
 
60
                        *                nh     = new height
 
61
                        * Returns: nothinh
 
62
                        */
 
63
                  void Pic::set_size(const unsigned short nw, const unsigned short nh) {
 
64
                                         if(pixels) free(pixels);
 
65
                                         w=nw;
 
66
                                         h=nh;
 
67
 
 
68
                                         // We make sure, that everything pic is aligned to 4bytes, so that clearing can 
 
69
                                         // goes fast with dwords
 
70
                                         if(w & 1) w++; // %2
 
71
                                         if(h & 1) h++;  // %2
 
72
                                         if(!w) w=2;
 
73
                                         if(!h) h=2;
 
74
 
 
75
                                         pixels=(unsigned short*) malloc(sizeof(short)*w*h);
 
76
 
 
77
                                         w=nw;
 
78
                                         h=nh;
 
79
 
 
80
                                         clear_all();
 
81
                  }
 
82
 
 
83
                  /** void Pic::clear_all(void) 
 
84
                        *
 
85
                        * This function clear all the pixels and sets them to the clrkey, if defined
 
86
                        *
 
87
                        * Args: none
 
88
                        * Returns: nothing
 
89
                        */
 
90
                  void Pic::clear_all(void) {
 
91
                                         if(!clrkey) return;
 
92
 
 
93
                                         for(unsigned int i=(w*h-2); i; i-=2) {
 
94
                                                                pixels[i]=clrkey;
 
95
                                         }
 
96
                  }
 
97
 
 
98
                  /** int Pic::load(const char* file)
 
99
                        *
 
100
                        * This function loads a bitmap from a file using the SDL functions 
 
101
                        *
 
102
                        * Args: file            Path to pic
 
103
                        * Returns: RET_OK or ERR_FAILED
 
104
                        */
 
105
                  int Pic::load(const char* file) {
 
106
                                         if(!file) return ERR_FAILED;
 
107
                                         
 
108
                                         SDL_Surface* bmp=NULL;
 
109
                                         int x=0;
 
110
                                         int y=0;
 
111
                                         unsigned char* bits, R, G, B;
 
112
 
 
113
                                         bmp=SDL_LoadBMP(file);
 
114
                                         if(bmp == NULL) { return ERR_FAILED; }
 
115
 
 
116
                                         set_size(bmp->w, bmp->h);
 
117
 
 
118
                                         for(y=0; y<h; y++) {
 
119
                                                                for(x=0; x<w; x++) {
 
120
                                                                                  bits=((Uint8*)bmp->pixels)+y*bmp->pitch+x*bmp->format->BytesPerPixel;
 
121
                                                                                  R= (Uint8) *((bits)+bmp->format->Rshift/8);
 
122
                                                                                  G= (Uint8) *((bits)+bmp->format->Gshift/8);
 
123
                                                                                  B= (Uint8) *((bits)+bmp->format->Bshift/8);
 
124
                                                                                  set_pixel(x, y, R, G, B);
 
125
                                                                }
 
126
                                         }
 
127
 
 
128
                                         SDL_FreeSurface(bmp);
 
129
 
 
130
                                         return RET_OK;
 
131
                  }
 
132
 
 
133
                  /** void Pic::set_clrkey(unsigned short dclrkey) 
 
134
                        *
 
135
                        * sets the clrkey of this pic
 
136
                        *
 
137
                        * Args: dclrkey The clrkey to use
 
138
                        * Returns: nothing
 
139
                        */
 
140
                  void Pic::set_clrkey(unsigned short dclrkey) {
 
141
                                         sh_clrkey=dclrkey;
 
142
                                         clrkey= (dclrkey<<16 | dclrkey);
 
143
                                         bhas_clrkey=true;
 
144
                  }
 
145
 
 
146
                  /** void Pic::set_clrkey(unsigned char r, unsigned char g, unsigned char b) 
 
147
                        *
 
148
                        * sets the clrkey of this pic
 
149
                        *
 
150
                        * Args: r       red value of clrkey
 
151
                        *                 g     green value of clrkey
 
152
                        *                 b     blue value of clrkey
 
153
                        * Returns: nothing
 
154
                        */
 
155
                  void Pic::set_clrkey(unsigned char r, unsigned char g, unsigned char b) { 
 
156
                                         unsigned short dclrkey=pack_rgb(r,g,b);
 
157
                                         sh_clrkey=dclrkey;
 
158
                                         clrkey= (dclrkey<<16 & clrkey);
 
159
                                         bhas_clrkey=true;
 
160
                  }     
 
161
 
 
162
}