gbasys

annotate src/gfx.c @ 8:047c61960005

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