Diffstat (limited to 'noncore/games/sfcave-sdl/bfont.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/games/sfcave-sdl/bfont.cpp | 7 |
1 files changed, 1 insertions, 6 deletions
diff --git a/noncore/games/sfcave-sdl/bfont.cpp b/noncore/games/sfcave-sdl/bfont.cpp index 0f29104..7dec8f5 100644 --- a/noncore/games/sfcave-sdl/bfont.cpp +++ b/noncore/games/sfcave-sdl/bfont.cpp | |||
@@ -28,155 +28,150 @@ void BFont::InitFont() | |||
28 | Chars[i].y = 1; | 28 | Chars[i].y = 1; |
29 | Chars[i].h = Surface->h; | 29 | Chars[i].h = Surface->h; |
30 | for (; GetPixel(x, 0) != sentry && x < (Surface->w); ++x) ; | 30 | for (; GetPixel(x, 0) != sentry && x < (Surface->w); ++x) ; |
31 | Chars[i].w = (x - Chars[i].x); | 31 | Chars[i].w = (x - Chars[i].x); |
32 | i++; | 32 | i++; |
33 | } else | 33 | } else |
34 | { x++; } | 34 | { x++; } |
35 | } | 35 | } |
36 | Chars[' '].x = 0; | 36 | Chars[' '].x = 0; |
37 | Chars[' '].y = 0; | 37 | Chars[' '].y = 0; |
38 | Chars[' '].h = Surface->h; | 38 | Chars[' '].h = Surface->h; |
39 | Chars[' '].w = Chars['!'].w; | 39 | Chars[' '].w = Chars['!'].w; |
40 | 40 | ||
41 | if (SDL_MUSTLOCK(Surface)) | 41 | if (SDL_MUSTLOCK(Surface)) |
42 | SDL_UnlockSurface(Surface); | 42 | SDL_UnlockSurface(Surface); |
43 | 43 | ||
44 | h = Surface->h; | 44 | h = Surface->h; |
45 | 45 | ||
46 | SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, GetPixel(0, Surface->h-1)); | 46 | SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, GetPixel(0, Surface->h-1)); |
47 | } | 47 | } |
48 | 48 | ||
49 | 49 | ||
50 | /* Load the font and stores it in the BFont_Info structure */ | 50 | /* Load the font and stores it in the BFont_Info structure */ |
51 | void BFont::LoadFont (const char *filename) | 51 | void BFont::LoadFont (const char *filename) |
52 | { | 52 | { |
53 | SDL_Surface *surface(NULL); | 53 | SDL_Surface *surface(NULL); |
54 | int x; | 54 | int x; |
55 | // tutta roba inutile in C++.... :-) | 55 | // tutta roba inutile in C++.... :-) |
56 | /* BFont_Info *Font=NULL; | 56 | /* BFont_Info *Font=NULL; |
57 | Font = (BFont_Info *) malloc(sizeof(BFont_Info));*/ | 57 | Font = (BFont_Info *) malloc(sizeof(BFont_Info));*/ |
58 | 58 | ||
59 | if ((filename != NULL ) && (this != NULL)) | 59 | if ((filename != NULL ) && (this != NULL)) |
60 | { | 60 | { |
61 | surface = IMG_Load( filename ); | 61 | surface = IMG_Load( filename ); |
62 | 62 | ||
63 | if (surface != NULL) | 63 | if (surface != NULL) |
64 | { | 64 | { |
65 | Surface = surface; | 65 | Surface = surface; |
66 | for (x=0; x<256; x++) | 66 | for (x=0; x<256; x++) |
67 | { | 67 | { |
68 | Chars[x].x = 0; | 68 | Chars[x].x = 0; |
69 | Chars[x].y = 0; | 69 | Chars[x].y = 0; |
70 | Chars[x].h = 0; | 70 | Chars[x].h = 0; |
71 | Chars[x].w = 0; | 71 | Chars[x].w = 0; |
72 | } | 72 | } |
73 | InitFont(); // Init the font | 73 | InitFont(); // Init the font |
74 | } | 74 | } |
75 | } | 75 | } |
76 | else | ||
77 | { | ||
78 | cerr << "Error! The font has not been loaded!" << endl; | ||
79 | } | ||
80 | } | 76 | } |
81 | 77 | ||
82 | BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b) | 78 | BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b) |
83 | { | 79 | { |
84 | int x,y; | 80 | int x,y; |
85 | 81 | ||
86 | BFont *newfont; | 82 | BFont *newfont; |
87 | SDL_Surface *surface = NULL; | 83 | SDL_Surface *surface = NULL; |
88 | 84 | ||
89 | Uint32 pixel; | 85 | Uint32 pixel; |
90 | Uint8 old_r, old_g, old_b; | 86 | Uint8 old_r, old_g, old_b; |
91 | Uint8 new_r, new_g, new_b; | 87 | Uint8 new_r, new_g, new_b; |
92 | Uint32 color_key; | 88 | Uint32 color_key; |
93 | 89 | ||
94 | newfont = new BFont(NULL); | 90 | newfont = new BFont(NULL); |
95 | 91 | ||
96 | if (newfont != NULL) { | 92 | if (newfont != NULL) { |
97 | 93 | ||
98 | newfont->h = h; | 94 | newfont->h = h; |
99 | 95 | ||
100 | for (x=0; x<256; x++) { | 96 | for (x=0; x<256; x++) { |
101 | newfont->Chars[x].x = Chars[x].x; | 97 | newfont->Chars[x].x = Chars[x].x; |
102 | newfont->Chars[x].y = Chars[x].y; | 98 | newfont->Chars[x].y = Chars[x].y; |
103 | newfont->Chars[x].h = Chars[x].h; | 99 | newfont->Chars[x].h = Chars[x].h; |
104 | newfont->Chars[x].w = Chars[x].w; | 100 | newfont->Chars[x].w = Chars[x].w; |
105 | } | 101 | } |
106 | 102 | ||
107 | surface = SDL_CreateRGBSurface(SDL_SWSURFACE, Surface->w, Surface->h, 32, | 103 | surface = SDL_CreateRGBSurface(SDL_SWSURFACE, Surface->w, Surface->h, 32, |
108 | 0x000000ff,0x0000ff00, 0x00ff0000, 0xff000000); | 104 | 0x000000ff,0x0000ff00, 0x00ff0000, 0xff000000); |
109 | if (surface != NULL) { | 105 | if (surface != NULL) { |
110 | 106 | ||
111 | if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface); | 107 | if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface); |
112 | if (SDL_MUSTLOCK(Surface)) SDL_LockSurface(Surface); | 108 | if (SDL_MUSTLOCK(Surface)) SDL_LockSurface(Surface); |
113 | 109 | ||
114 | color_key = xGetPixel(Surface, 0, Surface->h-1); | 110 | color_key = xGetPixel(Surface, 0, Surface->h-1); |
115 | 111 | ||
116 | printf("looking...\n"); | ||
117 | for( x=0; x < Surface->w; x++) { | 112 | for( x=0; x < Surface->w; x++) { |
118 | for( y=0; y < Surface->h; y++) { | 113 | for( y=0; y < Surface->h; y++) { |
119 | old_r = old_g = old_b = 0; | 114 | old_r = old_g = old_b = 0; |
120 | pixel = xGetPixel(Surface,x,y); | 115 | pixel = xGetPixel(Surface,x,y); |
121 | 116 | ||
122 | if (pixel != color_key) { | 117 | if (pixel != color_key) { |
123 | SDL_GetRGB(pixel, Surface->format, &old_r,&old_g,&old_b); | 118 | SDL_GetRGB(pixel, Surface->format, &old_r,&old_g,&old_b); |
124 | 119 | ||
125 | new_r = (Uint8) ((old_r * r) / 255); | 120 | new_r = (Uint8) ((old_r * r) / 255); |
126 | new_g = (Uint8) ((old_g * g) / 255); | 121 | new_g = (Uint8) ((old_g * g) / 255); |
127 | new_b = (Uint8) ((old_b * b) / 255); | 122 | new_b = (Uint8) ((old_b * b) / 255); |
128 | 123 | ||
129 | pixel = SDL_MapRGB(surface->format,new_r,new_g,new_b); | 124 | pixel = SDL_MapRGB(surface->format,new_r,new_g,new_b); |
130 | } | 125 | } |
131 | PutPixel(surface,x,y,pixel); | 126 | PutPixel(surface,x,y,pixel); |
132 | } | 127 | } |
133 | } | 128 | } |
134 | printf("unlooking...\n"); | 129 | |
135 | if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); | 130 | if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); |
136 | if (SDL_MUSTLOCK(Surface)) SDL_UnlockSurface(Surface); | 131 | if (SDL_MUSTLOCK(Surface)) SDL_UnlockSurface(Surface); |
137 | 132 | ||
138 | SDL_SetColorKey(surface, SDL_SRCCOLORKEY, color_key); | 133 | SDL_SetColorKey(surface, SDL_SRCCOLORKEY, color_key); |
139 | } | 134 | } |
140 | 135 | ||
141 | newfont->Surface = surface; | 136 | newfont->Surface = surface; |
142 | } | 137 | } |
143 | return newfont; | 138 | return newfont; |
144 | } | 139 | } |
145 | 140 | ||
146 | 141 | ||
147 | /* Puts a single char on the surface with the specified font */ | 142 | /* Puts a single char on the surface with the specified font */ |
148 | int BFont::PutChar(SDL_Surface *screen, int x, int y, char c) | 143 | int BFont::PutChar(SDL_Surface *screen, int x, int y, char c) |
149 | { | 144 | { |
150 | int r=0; | 145 | int r=0; |
151 | SDL_Rect dest; | 146 | SDL_Rect dest; |
152 | 147 | ||
153 | dest.w = CharWidth(' '); | 148 | dest.w = CharWidth(' '); |
154 | dest.h = FontHeight(); | 149 | dest.h = FontHeight(); |
155 | dest.x = x; | 150 | dest.x = x; |
156 | dest.y = y; | 151 | dest.y = y; |
157 | 152 | ||
158 | if (c != ' ') | 153 | if (c != ' ') |
159 | SDL_BlitSurface( Surface, &Chars[c], screen, &dest); | 154 | SDL_BlitSurface( Surface, &Chars[c], screen, &dest); |
160 | 155 | ||
161 | r = dest.w; | 156 | r = dest.w; |
162 | return r; | 157 | return r; |
163 | } | 158 | } |
164 | 159 | ||
165 | 160 | ||
166 | void BFont::PutString(SDL_Surface *screen, int x, int y, const char *text) | 161 | void BFont::PutString(SDL_Surface *screen, int x, int y, const char *text) |
167 | { | 162 | { |
168 | int i(0); | 163 | int i(0); |
169 | while (text[i]!='\0') | 164 | while (text[i]!='\0') |
170 | { | 165 | { |
171 | x += PutChar(screen,x,y,text[i]); | 166 | x += PutChar(screen,x,y,text[i]); |
172 | i++; | 167 | i++; |
173 | } | 168 | } |
174 | } | 169 | } |
175 | 170 | ||
176 | int BFont::TextWidth(const char *text) | 171 | int BFont::TextWidth(const char *text) |
177 | { | 172 | { |
178 | int i(0),x(0); | 173 | int i(0),x(0); |
179 | 174 | ||
180 | while (text[i]!='\0') | 175 | while (text[i]!='\0') |
181 | { | 176 | { |
182 | x += CharWidth(text[i]); | 177 | x += CharWidth(text[i]); |