gbasys

view src/gfx.c @ 6:f77381b12726

palette
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 04 Sep 2012 05:05:50 +0300
parents 06726f0b8cd3
children 72c6429ae953 047c61960005
line source
1 /*
2 Copyright 2004-2012 John Tsiombikas <nuclear@member.fsf.org>
4 This file is part of gbasys, a library for GameBoy Advance development.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include "gfx.h"
27 #define FRAME_SEL_BIT 0x10
29 #define H_BLANK_OAM 0x20
30 #define OBJ_MAP_2D 0x0
31 #define OBJ_MAP_1D 0x40
32 #define FORCE_BLANK 0x80
33 #define BG0_ENABLE 0x100
34 #define BG1_ENABLE 0x200
35 #define BG2_ENABLE 0x400
36 #define BG3_ENABLE 0x800
38 static volatile unsigned short *reg_disp_ctl = (void*)0x4000000;
39 static volatile unsigned short *reg_vcount = (void*)0x4000006;
41 static unsigned short *paladdr = (void*)0x5000000;
43 static int xres, yres;
44 static int sizeof_pixel;
45 static int page_flipping;
46 static int allocated_back_buffer;
48 static struct pixel_buffer fbuf, bbuf;
49 struct pixel_buffer *front_buffer = &fbuf;
50 struct pixel_buffer *back_buffer = &bbuf;
52 #define show_page(n) ((n) ? (*reg_disp_ctl |= FRAME_SEL_BIT) : (*reg_disp_ctl &= ~FRAME_SEL_BIT))
53 #define swap_page() (*reg_disp_ctl ^= FRAME_SEL_BIT)
55 int set_video_mode(int mode, int double_buffering) {
56 if(mode < 3 || mode > 5) return -1;
58 /* mode 5: 160x128, otherwise: 240x160 */
59 xres = (mode == 5) ? 160 : 240;
60 yres = (mode == 5) ? 128 : 160;
62 sizeof_pixel = (mode == 4) ? 1 : 2;
64 *reg_disp_ctl = mode | BG2_ENABLE;
66 show_page(0);
68 if(allocated_back_buffer) {
69 free(back_buffer->pixels);
70 }
72 front_buffer->pixels = (void*)0x6000000;
73 front_buffer->x = back_buffer->x = xres;
74 front_buffer->y = back_buffer->y = yres;
75 front_buffer->bpp = back_buffer->bpp = sizeof_pixel * 8;
77 if(mode > 3) {
78 page_flipping = 1;
79 back_buffer->pixels = (void*)0x600a000;
80 } else {
81 page_flipping = 0;
82 if(double_buffering) {
83 back_buffer->pixels = malloc(xres * yres * sizeof_pixel);
84 }
85 }
86 }
88 void flip(void) {
89 static void *tmp;
91 if(page_flipping) {
92 swap_page();
93 tmp = front_buffer->pixels;
94 front_buffer->pixels = back_buffer->pixels;
95 back_buffer->pixels = tmp;
96 } else {
97 /*dma_copy32(3, front_buffer->pixels, back_buffer->pixels, (xres * yres) >> 1);*/
98 dma_copy32(3, front_buffer->pixels, back_buffer->pixels, 19200);
99 }
100 }
102 /* ------- pixel buffer operations ------- */
104 struct pixel_buffer *create_pixel_buffer(int x, int y, int bpp) {
105 struct pixel_buffer *pbuf = malloc(sizeof(struct pixel_buffer));
106 if(pbuf) {
107 pbuf->pixels = malloc(x * y * bpp / 8);
108 pbuf->x = x;
109 pbuf->y = y;
110 pbuf->bpp = bpp;
111 }
112 return pbuf;
113 }
115 void destroy_pixel_buffer(struct pixel_buffer *pbuf) {
116 free(pbuf->pixels);
117 free(pbuf);
118 }
120 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color) {
121 int sz = pbuf->x * pbuf->y;
123 if(pbuf->bpp == 8) {
124 color |= color << 8;
125 sz >>= 1;
126 }
128 dma_fill16(3, pbuf->pixels, color, sz);
129 }
131 void copy_buffer(const struct pixel_buffer *src, struct pixel_buffer *dst) {
132 int words;
134 if(src->x != dst->x || src->y != dst->y || src->bpp != dst->bpp) return;
136 words = (src->x * src->y) >> (src->bpp == 16 ? 1 : 2);
137 dma_copy32(3, dst->pixels, src->pixels, words);
138 }
140 #define MIN(a, b) ((a) < (b) ? (a) : (b))
142 void blit(struct pixel_buffer *src, int src_x, int src_y, int src_w, int src_h,
143 struct pixel_buffer *dst, int dst_x, int dst_y)
144 {
145 int i, pixsize, width, height, dstride, sstride;
146 unsigned char *dptr, *sptr;
148 if(dst->bpp != src->bpp)
149 return;
151 if(src_w <= 0)
152 src_w = src->x;
153 if(src_h <= 0)
154 src_h = src->y;
156 width = MIN(src_w, MIN(src->x - src_x, dst->x - dst_x));
157 height = MIN(src_h, MIN(src->y - src_y, dst->y - dst_y));
159 if(width <= 0 || height <= 0)
160 return;
162 pixsize = dst->bpp / 8;
163 dptr = (unsigned char*)dst->pixels + (dst_y * dst->x + dst_x) * pixsize;
164 sptr = (unsigned char*)src->pixels + (src_y * src->x + src_x) * pixsize;
166 dstride = dst->x * pixsize;
167 sstride = src->x * pixsize;
169 for(i=0; i<height; i++) {
170 memcpy(dptr, sptr, width * pixsize);
171 sptr += sstride;
172 dptr += dstride;
173 }
174 }
176 void set_palette(int idx, int r, int g, int b)
177 {
178 paladdr[idx] = RGB(r, g, b);
179 }
181 void draw_line(int x1, int y1, int x2, int y2, unsigned short col, struct pixel_buffer *pbuf) {
182 int dx, dy, dx2, dy2;
183 int x_inc, y_inc;
184 int error;
185 int i;
186 unsigned short *ptr = pbuf->pixels;
188 ptr += y1 * pbuf->x + x1;
189 dx = x2 - x1;
190 dy = y2 - y1;
192 if(dx >= 0) {
193 x_inc = 1;
194 } else {
195 x_inc = -1;
196 dx = -dx;
197 }
199 if(dy >= 0) {
200 y_inc = pbuf->x;
201 } else {
202 y_inc = -pbuf->x;
203 dy = -dy;
204 }
206 dx2 = dx << 1;
207 dy2 = dy << 1;
209 if(dx > dy) {
210 error = dy2 - dx;
211 for(i=0; i<=dx; i++) {
212 *ptr = col;
213 if(error >= 0) {
214 error -= dx2;
215 ptr += y_inc;
216 }
217 error += dy2;
218 ptr += x_inc;
219 }
220 } else {
221 error = dx2 - dy;
222 for(i=0;i<=dy;i++) {
223 *ptr = col;
224 if(error >= 0) {
225 error -= dy2;
226 ptr += x_inc;
227 }
228 error += dx2;
229 ptr += y_inc;
230 }
231 }
232 }