gbasys

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