1 /*
2  * OpenTyrian: A modern cross-platform port of Tyrian
3  * Copyright (C) 2007-2009  The OpenTyrian Development Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 #include "config.h" // For fullscreen stuff
20 #include "keyboard.h"
21 #include "opentyr.h"
22 #include "palette.h"
23 #include "vga256d.h"
24 #include "video.h"
25 
26 #include "SDL.h"
27 #include <assert.h>
28 #include <ctype.h>
29 #include <math.h>
30 #include <stdio.h>
31 #include <string.h>
32 
JE_pix(SDL_Surface * surface,int x,int y,JE_byte c)33 void JE_pix( SDL_Surface *surface, int x, int y, JE_byte c )
34 {
35 	/* Bad things happen if we don't clip */
36 	if (x <  surface->pitch && y <  surface->h)
37 	{
38 		Uint8 *vga = surface->pixels;
39 		vga[y * surface->pitch + x] = c;
40 	}
41 }
42 
JE_pix3(SDL_Surface * surface,int x,int y,JE_byte c)43 void JE_pix3( SDL_Surface *surface, int x, int y, JE_byte c )
44 {
45 	/* Originally impemented as several direct accesses */
46 	JE_pix(surface, x, y, c);
47 	JE_pix(surface, x - 1, y, c);
48 	JE_pix(surface, x + 1, y, c);
49 	JE_pix(surface, x, y - 1, c);
50 	JE_pix(surface, x, y + 1, c);
51 }
52 
JE_rectangle(SDL_Surface * surface,int a,int b,int c,int d,int e)53 void JE_rectangle( SDL_Surface *surface, int a, int b, int c, int d, int e ) /* x1, y1, x2, y2, color */
54 {
55 	if (a < surface->pitch && b < surface->h &&
56 	    c < surface->pitch && d < surface->h)
57 	{
58 		Uint8 *vga = surface->pixels;
59 		int i;
60 
61 		/* Top line */
62 		memset(&vga[b * surface->pitch + a], e, c - a + 1);
63 
64 		/* Bottom line */
65 		memset(&vga[d * surface->pitch + a], e, c - a + 1);
66 
67 		/* Left line */
68 		for (i = (b + 1) * surface->pitch + a; i < (d * surface->pitch + a); i += surface->pitch)
69 		{
70 			vga[i] = e;
71 		}
72 
73 		/* Right line */
74 		for (i = (b + 1) * surface->pitch + c; i < (d * surface->pitch + c); i += surface->pitch)
75 		{
76 			vga[i] = e;
77 		}
78 	} else {
79 		printf("!!! WARNING: Rectangle clipped: %d %d %d %d %d\n", a, b, c, d, e);
80 	}
81 }
82 
fill_rectangle_xy(SDL_Surface * surface,int x,int y,int x2,int y2,Uint8 color)83 void fill_rectangle_xy( SDL_Surface *surface, int x, int y, int x2, int y2, Uint8 color )
84 {
85 	SDL_Rect rect = { x, y, x2 - x + 1, y2 - y + 1 };
86 	SDL_FillRect(surface, &rect, color);
87 }
88 
JE_barShade(SDL_Surface * surface,int a,int b,int c,int d)89 void JE_barShade( SDL_Surface *surface, int a, int b, int c, int d ) /* x1, y1, x2, y2 */
90 {
91 	if (a < surface->pitch && b < surface->h &&
92 	    c < surface->pitch && d < surface->h)
93 	{
94 		Uint8 *vga = surface->pixels;
95 		int i, j, width;
96 
97 		width = c - a + 1;
98 
99 		for (i = b * surface->pitch + a; i <= d * surface->pitch + a; i += surface->pitch)
100 		{
101 			for (j = 0; j < width; j++)
102 			{
103 				vga[i + j] = ((vga[i + j] & 0x0F) >> 1) | (vga[i + j] & 0xF0);
104 			}
105 		}
106 	} else {
107 		printf("!!! WARNING: Darker Rectangle clipped: %d %d %d %d\n", a,b,c,d);
108 	}
109 }
110 
JE_barBright(SDL_Surface * surface,int a,int b,int c,int d)111 void JE_barBright( SDL_Surface *surface, int a, int b, int c, int d ) /* x1, y1, x2, y2 */
112 {
113 	if (a < surface->pitch && b < surface->h &&
114 	    c < surface->pitch && d < surface->h)
115 	{
116 		Uint8 *vga = surface->pixels;
117 		int i, j, width;
118 
119 		width = c-a+1;
120 
121 		for (i = b * surface->pitch + a; i <= d * surface->pitch + a; i += surface->pitch)
122 		{
123 			for (j = 0; j < width; j++)
124 			{
125 				JE_byte al, ah;
126 				al = ah = vga[i + j];
127 
128 				ah &= 0xF0;
129 				al = (al & 0x0F) + 2;
130 
131 				if (al > 0x0F)
132 				{
133 					al = 0x0F;
134 				}
135 
136 				vga[i + j] = al + ah;
137 			}
138 		}
139 	} else {
140 		printf("!!! WARNING: Brighter Rectangle clipped: %d %d %d %d\n", a,b,c,d);
141 	}
142 }
143 
draw_segmented_gauge(SDL_Surface * surface,int x,int y,Uint8 color,uint segment_width,uint segment_height,uint segment_value,uint value)144 void draw_segmented_gauge( SDL_Surface *surface, int x, int y, Uint8 color, uint segment_width, uint segment_height, uint segment_value, uint value )
145 {
146 	assert(segment_width > 0 && segment_height > 0);
147 
148 	const uint segments = value / segment_value,
149 	           partial_segment = value % segment_value;
150 
151 	for (uint i = 0; i < segments; ++i)
152 	{
153 		fill_rectangle_hw(surface, x, y, segment_width, segment_height, color + 12);
154 		x += segment_width + 1;
155 	}
156 	if (partial_segment > 0)
157 		fill_rectangle_hw(surface, x, y, segment_width, segment_height, color + (12 * partial_segment / segment_value));
158 }
159 
160