| 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 "vga256d.h" |
| 20 | |
| 21 | #include "config.h" // For fullscreen stuff |
| 22 | #include "keyboard.h" |
| 23 | #include "opentyr.h" |
| 24 | #include "palette.h" |
| 25 | #include "video.h" |
| 26 | |
| 27 | #include "SDL.h" |
| 28 | |
| 29 | #include <assert.h> |
| 30 | #include <ctype.h> |
| 31 | #include <math.h> |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | void JE_pix(SDL_Surface *surface, int x, int y, JE_byte c) |
| 36 | { |
| 37 | /* Bad things happen if we don't clip */ |
| 38 | if (x < surface->pitch && y < surface->h) |
| 39 | { |
| 40 | Uint8 *vga = surface->pixels; |
| 41 | vga[y * surface->pitch + x] = c; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void JE_pix3(SDL_Surface *surface, int x, int y, JE_byte c) |
| 46 | { |
| 47 | /* Originally implemented as several direct accesses */ |
| 48 | JE_pix(surface, x, y, c); |
| 49 | JE_pix(surface, x - 1, y, c); |
| 50 | JE_pix(surface, x + 1, y, c); |
| 51 | JE_pix(surface, x, y - 1, c); |
| 52 | JE_pix(surface, x, y + 1, c); |
| 53 | } |
| 54 | |
| 55 | void JE_rectangle(SDL_Surface *surface, int a, int b, int c, int d, int e) /* x1, y1, x2, y2, color */ |
| 56 | { |
| 57 | if (a < surface->pitch && b < surface->h && |
| 58 | c < surface->pitch && d < surface->h) |
| 59 | { |
| 60 | Uint8 *vga = surface->pixels; |
| 61 | int i; |
| 62 | |
| 63 | /* Top line */ |
| 64 | memset(&vga[b * surface->pitch + a], e, c - a + 1); |
| 65 | |
| 66 | /* Bottom line */ |
| 67 | memset(&vga[d * surface->pitch + a], e, c - a + 1); |
| 68 | |
| 69 | /* Left line */ |
| 70 | for (i = (b + 1) * surface->pitch + a; i < (d * surface->pitch + a); i += surface->pitch) |
| 71 | { |
| 72 | vga[i] = e; |
| 73 | } |
| 74 | |
| 75 | /* Right line */ |
| 76 | for (i = (b + 1) * surface->pitch + c; i < (d * surface->pitch + c); i += surface->pitch) |
| 77 | { |
| 78 | vga[i] = e; |
| 79 | } |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | printf("!!! WARNING: Rectangle clipped: %d %d %d %d %d\n", a, b, c, d, e); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void fill_rectangle_xy(SDL_Surface *surface, int x, int y, int x2, int y2, Uint8 color) |
| 88 | { |
| 89 | SDL_Rect rect = { x, y, x2 - x + 1, y2 - y + 1 }; |
| 90 | SDL_FillRect(surface, &rect, color); |
| 91 | } |
| 92 | |
| 93 | void JE_barShade(SDL_Surface *surface, int a, int b, int c, int d) /* x1, y1, x2, y2 */ |
| 94 | { |
| 95 | if (a < surface->pitch && b < surface->h && |
| 96 | c < surface->pitch && d < surface->h) |
| 97 | { |
| 98 | Uint8 *vga = surface->pixels; |
| 99 | int i, j, width; |
| 100 | |
| 101 | width = c - a + 1; |
| 102 | |
| 103 | for (i = b * surface->pitch + a; i <= d * surface->pitch + a; i += surface->pitch) |
| 104 | { |
| 105 | for (j = 0; j < width; j++) |
| 106 | { |
| 107 | vga[i + j] = ((vga[i + j] & 0x0F) >> 1) | (vga[i + j] & 0xF0); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | printf("!!! WARNING: Darker Rectangle clipped: %d %d %d %d\n", a,b,c,d); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void JE_barBright(SDL_Surface *surface, int a, int b, int c, int d) /* x1, y1, x2, y2 */ |
| 118 | { |
| 119 | if (a < surface->pitch && b < surface->h && |
| 120 | c < surface->pitch && d < surface->h) |
| 121 | { |
| 122 | Uint8 *vga = surface->pixels; |
| 123 | int i, j, width; |
| 124 | |
| 125 | width = c-a+1; |
| 126 | |
| 127 | for (i = b * surface->pitch + a; i <= d * surface->pitch + a; i += surface->pitch) |
| 128 | { |
| 129 | for (j = 0; j < width; j++) |
| 130 | { |
| 131 | JE_byte al, ah; |
| 132 | al = ah = vga[i + j]; |
| 133 | |
| 134 | ah &= 0xF0; |
| 135 | al = (al & 0x0F) + 2; |
| 136 | |
| 137 | if (al > 0x0F) |
| 138 | { |
| 139 | al = 0x0F; |
| 140 | } |
| 141 | |
| 142 | vga[i + j] = al + ah; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | printf("!!! WARNING: Brighter Rectangle clipped: %d %d %d %d\n", a,b,c,d); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void draw_segmented_gauge(SDL_Surface *surface, int x, int y, Uint8 color, uint segment_width, uint segment_height, uint segment_value, uint value) |
| 153 | { |
| 154 | assert(segment_width > 0 && segment_height > 0); |
| 155 | |
| 156 | const uint segments = value / segment_value, |
| 157 | partial_segment = value % segment_value; |
| 158 | |
| 159 | for (uint i = 0; i < segments; ++i) |
| 160 | { |
| 161 | fill_rectangle_wh(surface, x, y, segment_width, segment_height, color + 12); |
| 162 | x += segment_width + 1; |
| 163 | } |
| 164 | if (partial_segment > 0) |
| 165 | fill_rectangle_wh(surface, x, y, segment_width, segment_height, color + (12 * partial_segment / segment_value)); |
| 166 | } |