gbasys

view src/gfx.c @ 9:85f219fcdc82

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Jun 2014 06:27:18 +0300
parents 047c61960005 72c6429ae953
children a6ddf338a111
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 }