gbasys

view src/gfx.c @ 12:a6ddf338a111

line clipping
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 01 Feb 2016 04:41:27 +0200
parents 85f219fcdc82
children
line source
1 /*
2 gbasys - a gameboy advance hardware abstraction library
3 Copyright (C) 2004-2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "config.h"
20 #include <stdlib.h>
21 #include <string.h>
22 #include "gfx.h"
24 #define FRAME_SEL_BIT 0x10
26 #define H_BLANK_OAM 0x20
27 #define OBJ_MAP_2D 0x0
28 #define OBJ_MAP_1D 0x40
29 #define FORCE_BLANK 0x80
30 #define BG0_ENABLE 0x100
31 #define BG1_ENABLE 0x200
32 #define BG2_ENABLE 0x400
33 #define BG3_ENABLE 0x800
35 #define REG_DISPCTL (*(unsigned short*)0x4000000)
36 #define REG_VCOUNT (*(unsigned short*)0x4000006)
38 #define REG_BG2PA (*(short*)0x4000020)
39 #define REG_BG2PB (*(short*)0x4000022)
40 #define REG_BG2PC (*(short*)0x4000024)
41 #define REG_BG2PD (*(short*)0x4000026)
43 static unsigned short *paladdr = (void*)0x5000000;
45 static int xres, yres;
46 static int sizeof_pixel;
47 static int page_flipping;
48 static int allocated_back_buffer;
50 static struct pixel_buffer fbuf, bbuf;
51 struct pixel_buffer *front_buffer = &fbuf;
52 struct pixel_buffer *back_buffer = &bbuf;
54 #define show_page(n) ((n) ? (REG_DISPCTL |= FRAME_SEL_BIT) : (REG_DISPCTL &= ~FRAME_SEL_BIT))
55 #define swap_page() (REG_DISPCTL ^= FRAME_SEL_BIT)
57 int set_video_mode(int mode, int double_buffering) {
58 if(mode < 3 || mode > 5) return -1;
60 /* mode 5: 160x128, otherwise: 240x160 */
61 xres = (mode == 5) ? 160 : 240;
62 yres = (mode == 5) ? 128 : 160;
64 sizeof_pixel = (mode == 4) ? 1 : 2;
66 REG_DISPCTL = mode | BG2_ENABLE;
68 show_page(0);
70 if(allocated_back_buffer) {
71 free(back_buffer->pixels);
72 }
74 front_buffer->pixels = (void*)0x6000000;
75 front_buffer->x = back_buffer->x = xres;
76 front_buffer->y = back_buffer->y = yres;
77 front_buffer->bpp = back_buffer->bpp = sizeof_pixel * 8;
79 if(mode > 3) {
80 page_flipping = 1;
81 back_buffer->pixels = (void*)0x600a000;
82 } else {
83 page_flipping = 0;
84 if(double_buffering) {
85 back_buffer->pixels = malloc(xres * yres * sizeof_pixel);
86 }
87 }
88 }
90 void flip(void) {
91 static void *tmp;
93 if(page_flipping) {
94 swap_page();
95 tmp = front_buffer->pixels;
96 front_buffer->pixels = back_buffer->pixels;
97 back_buffer->pixels = tmp;
98 } else {
99 /*dma_copy32(3, front_buffer->pixels, back_buffer->pixels, (xres * yres) >> 1);*/
100 dma_copy32(3, front_buffer->pixels, back_buffer->pixels, 19200);
101 }
102 }
104 /* ------- pixel buffer operations ------- */
106 struct pixel_buffer *create_pixel_buffer(int x, int y, int bpp) {
107 struct pixel_buffer *pbuf = malloc(sizeof(struct pixel_buffer));
108 if(pbuf) {
109 pbuf->pixels = malloc(x * y * bpp / 8);
110 pbuf->x = x;
111 pbuf->y = y;
112 pbuf->bpp = bpp;
113 }
114 return pbuf;
115 }
117 void destroy_pixel_buffer(struct pixel_buffer *pbuf) {
118 free(pbuf->pixels);
119 free(pbuf);
120 }
122 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color) {
123 int sz = pbuf->x * pbuf->y;
125 if(pbuf->bpp == 8) {
126 color |= color << 8;
127 sz >>= 1;
128 }
130 dma_fill16(3, pbuf->pixels, color, sz);
131 }
133 void copy_buffer(const struct pixel_buffer *src, struct pixel_buffer *dst) {
134 int words;
136 if(src->x != dst->x || src->y != dst->y || src->bpp != dst->bpp) return;
138 words = (src->x * src->y) >> (src->bpp == 16 ? 1 : 2);
139 dma_copy32(3, dst->pixels, src->pixels, words);
140 }
142 #define MIN(a, b) ((a) < (b) ? (a) : (b))
144 void blit(struct pixel_buffer *src, int src_x, int src_y, int src_w, int src_h,
145 struct pixel_buffer *dst, int dst_x, int dst_y)
146 {
147 int i, pixsize, width, height, dstride, sstride;
148 unsigned char *dptr, *sptr;
150 if(dst->bpp != src->bpp)
151 return;
153 if(src_w <= 0)
154 src_w = src->x;
155 if(src_h <= 0)
156 src_h = src->y;
158 width = MIN(src_w, MIN(src->x - src_x, dst->x - dst_x));
159 height = MIN(src_h, MIN(src->y - src_y, dst->y - dst_y));
161 if(width <= 0 || height <= 0)
162 return;
164 pixsize = dst->bpp / 8;
165 dptr = (unsigned char*)dst->pixels + (dst_y * dst->x + dst_x) * pixsize;
166 sptr = (unsigned char*)src->pixels + (src_y * src->x + src_x) * pixsize;
168 dstride = dst->x * pixsize;
169 sstride = src->x * pixsize;
171 for(i=0; i<height; i++) {
172 memcpy(dptr, sptr, width * pixsize);
173 sptr += sstride;
174 dptr += dstride;
175 }
176 }
178 void set_palette(int idx, int r, int g, int b)
179 {
180 paladdr[idx] = RGB(r, g, b);
181 }
183 void set_bg_matrix(int a, int b, int c, int d)
184 {
185 REG_BG2PA = a;
186 REG_BG2PB = b;
187 REG_BG2PC = c;
188 REG_BG2PD = d;
189 }
191 void set_bg_scale(int x, int y)
192 {
193 set_bg_matrix(x, 0, 0, y);
194 }
196 void draw_line(int x1, int y1, int x2, int y2, unsigned short col, struct pixel_buffer *pbuf) {
197 int dx, dy, dx2, dy2;
198 int x_inc, y_inc;
199 int error;
200 int i;
201 unsigned short *ptr = pbuf->pixels;
203 ptr += y1 * pbuf->x + x1;
204 dx = x2 - x1;
205 dy = y2 - y1;
207 if(dx >= 0) {
208 x_inc = 1;
209 } else {
210 x_inc = -1;
211 dx = -dx;
212 }
214 if(dy >= 0) {
215 y_inc = pbuf->x;
216 } else {
217 y_inc = -pbuf->x;
218 dy = -dy;
219 }
221 dx2 = dx << 1;
222 dy2 = dy << 1;
224 if(dx > dy) {
225 error = dy2 - dx;
226 for(i=0; i<=dx; i++) {
227 *ptr = col;
228 if(error >= 0) {
229 error -= dx2;
230 ptr += y_inc;
231 }
232 error += dy2;
233 ptr += x_inc;
234 }
235 } else {
236 error = dx2 - dy;
237 for(i=0;i<=dy;i++) {
238 *ptr = col;
239 if(error >= 0) {
240 error -= dy2;
241 ptr += x_inc;
242 }
243 error += dx2;
244 ptr += y_inc;
245 }
246 }
247 }
249 enum {
250 OUT_L = 1,
251 OUT_R = 2,
252 OUT_T = 4,
253 OUT_B = 8
254 };
255 static unsigned int outcode(int x, int y, int left, int top, int right, int bot)
256 {
257 unsigned int res = 0;
258 if(x < left) res |= OUT_L;
259 if(x > right) res |= OUT_R;
260 if(y < top) res |= OUT_T;
261 if(y > bot) res |= OUT_B;
262 return res;
263 }
265 int clip_line(int *x1, int *y1, int *x2, int *y2, int left, int top, int right, int bot)
266 {
267 int i;
268 unsigned int out1, out2;
270 --right;
271 --bot;
272 out1 = outcode(*x1, *y1, left, top, right, bot);
273 out2 = outcode(*x2, *y2, left, top, right, bot);
275 for(;;) {
276 int x, y;
277 unsigned int outout;
279 if((out1 | out2) == 0)
280 return 1; /* both inside */
281 if(out1 & out2)
282 return 0; /* both outside in the same area */
284 outout = out1 ? out1 : out2;
285 if(outout & OUT_T) {
286 x = *x1 + (float)(*x2 - *x1) * (float)(top - *y1) / (float)(*y2 - *y1);
287 y = top;
288 } else if(outout & OUT_B) {
289 x = *x1 + (float)(*x2 - *x1) * (float)(bot - *y1) / (float)(*y2 - *y1);
290 y = bot;
291 } else if(outout & OUT_R) {
292 y = *y1 + (float)(*y2 - *y1) * (float)(right - *x1) / (float)(*x2 - *x1);
293 x = right;
294 } else if(outout & OUT_L) {
295 y = *y1 + (float)(*y2 - *y1) * (float)(left - *x1) / (float)(*x2 - *x1);
296 x = left;
297 }
299 if(out1) {
300 *x1 = x;
301 *y1 = y;
302 out1 = outcode(*x1, *y1, left, top, right, bot);
303 } else {
304 *x2 = x;
305 *y2 = y;
306 out2 = outcode(*x2, *y2, left, top, right, bot);
307 }
308 }
309 return 1;
310 }