gbasys

view src/gfx.c @ 7:72c6429ae953

changed all the copyright headers and added a README
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 18 Apr 2014 02:04:46 +0300
parents f77381b12726
children 85f219fcdc82
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 static volatile unsigned short *reg_disp_ctl = (void*)0x4000000;
36 static volatile unsigned short *reg_vcount = (void*)0x4000006;
38 static unsigned short *paladdr = (void*)0x5000000;
40 static int xres, yres;
41 static int sizeof_pixel;
42 static int page_flipping;
43 static int allocated_back_buffer;
45 static struct pixel_buffer fbuf, bbuf;
46 struct pixel_buffer *front_buffer = &fbuf;
47 struct pixel_buffer *back_buffer = &bbuf;
49 #define show_page(n) ((n) ? (*reg_disp_ctl |= FRAME_SEL_BIT) : (*reg_disp_ctl &= ~FRAME_SEL_BIT))
50 #define swap_page() (*reg_disp_ctl ^= FRAME_SEL_BIT)
52 int set_video_mode(int mode, int double_buffering) {
53 if(mode < 3 || mode > 5) return -1;
55 /* mode 5: 160x128, otherwise: 240x160 */
56 xres = (mode == 5) ? 160 : 240;
57 yres = (mode == 5) ? 128 : 160;
59 sizeof_pixel = (mode == 4) ? 1 : 2;
61 *reg_disp_ctl = mode | BG2_ENABLE;
63 show_page(0);
65 if(allocated_back_buffer) {
66 free(back_buffer->pixels);
67 }
69 front_buffer->pixels = (void*)0x6000000;
70 front_buffer->x = back_buffer->x = xres;
71 front_buffer->y = back_buffer->y = yres;
72 front_buffer->bpp = back_buffer->bpp = sizeof_pixel * 8;
74 if(mode > 3) {
75 page_flipping = 1;
76 back_buffer->pixels = (void*)0x600a000;
77 } else {
78 page_flipping = 0;
79 if(double_buffering) {
80 back_buffer->pixels = malloc(xres * yres * sizeof_pixel);
81 }
82 }
83 }
85 void flip(void) {
86 static void *tmp;
88 if(page_flipping) {
89 swap_page();
90 tmp = front_buffer->pixels;
91 front_buffer->pixels = back_buffer->pixels;
92 back_buffer->pixels = tmp;
93 } else {
94 /*dma_copy32(3, front_buffer->pixels, back_buffer->pixels, (xres * yres) >> 1);*/
95 dma_copy32(3, front_buffer->pixels, back_buffer->pixels, 19200);
96 }
97 }
99 /* ------- pixel buffer operations ------- */
101 struct pixel_buffer *create_pixel_buffer(int x, int y, int bpp) {
102 struct pixel_buffer *pbuf = malloc(sizeof(struct pixel_buffer));
103 if(pbuf) {
104 pbuf->pixels = malloc(x * y * bpp / 8);
105 pbuf->x = x;
106 pbuf->y = y;
107 pbuf->bpp = bpp;
108 }
109 return pbuf;
110 }
112 void destroy_pixel_buffer(struct pixel_buffer *pbuf) {
113 free(pbuf->pixels);
114 free(pbuf);
115 }
117 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color) {
118 int sz = pbuf->x * pbuf->y;
120 if(pbuf->bpp == 8) {
121 color |= color << 8;
122 sz >>= 1;
123 }
125 dma_fill16(3, pbuf->pixels, color, sz);
126 }
128 void copy_buffer(const struct pixel_buffer *src, struct pixel_buffer *dst) {
129 int words;
131 if(src->x != dst->x || src->y != dst->y || src->bpp != dst->bpp) return;
133 words = (src->x * src->y) >> (src->bpp == 16 ? 1 : 2);
134 dma_copy32(3, dst->pixels, src->pixels, words);
135 }
137 #define MIN(a, b) ((a) < (b) ? (a) : (b))
139 void blit(struct pixel_buffer *src, int src_x, int src_y, int src_w, int src_h,
140 struct pixel_buffer *dst, int dst_x, int dst_y)
141 {
142 int i, pixsize, width, height, dstride, sstride;
143 unsigned char *dptr, *sptr;
145 if(dst->bpp != src->bpp)
146 return;
148 if(src_w <= 0)
149 src_w = src->x;
150 if(src_h <= 0)
151 src_h = src->y;
153 width = MIN(src_w, MIN(src->x - src_x, dst->x - dst_x));
154 height = MIN(src_h, MIN(src->y - src_y, dst->y - dst_y));
156 if(width <= 0 || height <= 0)
157 return;
159 pixsize = dst->bpp / 8;
160 dptr = (unsigned char*)dst->pixels + (dst_y * dst->x + dst_x) * pixsize;
161 sptr = (unsigned char*)src->pixels + (src_y * src->x + src_x) * pixsize;
163 dstride = dst->x * pixsize;
164 sstride = src->x * pixsize;
166 for(i=0; i<height; i++) {
167 memcpy(dptr, sptr, width * pixsize);
168 sptr += sstride;
169 dptr += dstride;
170 }
171 }
173 void set_palette(int idx, int r, int g, int b)
174 {
175 paladdr[idx] = RGB(r, g, b);
176 }
178 void draw_line(int x1, int y1, int x2, int y2, unsigned short col, struct pixel_buffer *pbuf) {
179 int dx, dy, dx2, dy2;
180 int x_inc, y_inc;
181 int error;
182 int i;
183 unsigned short *ptr = pbuf->pixels;
185 ptr += y1 * pbuf->x + x1;
186 dx = x2 - x1;
187 dy = y2 - y1;
189 if(dx >= 0) {
190 x_inc = 1;
191 } else {
192 x_inc = -1;
193 dx = -dx;
194 }
196 if(dy >= 0) {
197 y_inc = pbuf->x;
198 } else {
199 y_inc = -pbuf->x;
200 dy = -dy;
201 }
203 dx2 = dx << 1;
204 dy2 = dy << 1;
206 if(dx > dy) {
207 error = dy2 - dx;
208 for(i=0; i<=dx; i++) {
209 *ptr = col;
210 if(error >= 0) {
211 error -= dx2;
212 ptr += y_inc;
213 }
214 error += dy2;
215 ptr += x_inc;
216 }
217 } else {
218 error = dx2 - dy;
219 for(i=0;i<=dy;i++) {
220 *ptr = col;
221 if(error >= 0) {
222 error -= dy2;
223 ptr += x_inc;
224 }
225 error += dx2;
226 ptr += y_inc;
227 }
228 }
229 }