Demo of low res colour vga video for stm32f3 discovery board

Dependencies:   STM32F3-Discovery-minimal

Fork of Space_Invaders_Demo by Martin Johnson

Committer:
MartinJohnson
Date:
Wed May 16 02:39:14 2018 +0000
Revision:
3:93e488fbb8a2
Parent:
0:404dae88af71
Child:
5:594c9712697c
working colour video output

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MartinJohnson 0:404dae88af71 1 /***************************************************************************
MartinJohnson 0:404dae88af71 2 * STM32 VGA demo
MartinJohnson 0:404dae88af71 3 * Copyright (C) 2012 Artekit Italy
MartinJohnson 0:404dae88af71 4 * http://www.artekit.eu
MartinJohnson 0:404dae88af71 5 * Written by Ruben H. Meleca
MartinJohnson 0:404dae88af71 6
MartinJohnson 0:404dae88af71 7 ### gdi.c
MartinJohnson 0:404dae88af71 8
MartinJohnson 0:404dae88af71 9 # This program is free software; you can redistribute it and/or modify
MartinJohnson 0:404dae88af71 10 # it under the terms of the GNU General Public License as published by
MartinJohnson 0:404dae88af71 11 # the Free Software Foundation; either version 2 of the License, or
MartinJohnson 0:404dae88af71 12 # (at your option) any later version.
MartinJohnson 0:404dae88af71 13 #
MartinJohnson 0:404dae88af71 14 # This program is distributed in the hope that it will be useful,
MartinJohnson 0:404dae88af71 15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
MartinJohnson 0:404dae88af71 16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MartinJohnson 0:404dae88af71 17 # GNU General Public License for more details.
MartinJohnson 0:404dae88af71 18 #
MartinJohnson 0:404dae88af71 19 # You should have received a copy of the GNU General Public License
MartinJohnson 0:404dae88af71 20 # along with this program; if not, write to the Free Software
MartinJohnson 0:404dae88af71 21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
MartinJohnson 0:404dae88af71 22
MartinJohnson 0:404dae88af71 23 ***************************************************************************/
MartinJohnson 0:404dae88af71 24
MartinJohnson 0:404dae88af71 25 #include "gdi.h"
MartinJohnson 0:404dae88af71 26 #include "mth.h"
MartinJohnson 0:404dae88af71 27 #include "video.h"
MartinJohnson 0:404dae88af71 28 #include "stm32f30x.h"
MartinJohnson 0:404dae88af71 29
MartinJohnson 0:404dae88af71 30 extern u8 *fb[VID_VSIZE];// *(VID_HSIZE+2)];
MartinJohnson 0:404dae88af71 31 extern const u8 gdiSystemFont[];
MartinJohnson 0:404dae88af71 32
MartinJohnson 0:404dae88af71 33 #define CCM __attribute__ ((section (".ccmram")))
MartinJohnson 0:404dae88af71 34 const u8 gdiCloseBm[] = { 0x7f, 0xC0,
MartinJohnson 0:404dae88af71 35 0x7f, 0xC0,
MartinJohnson 0:404dae88af71 36 0x7f, 0xC0,
MartinJohnson 0:404dae88af71 37 0x7f, 0xC0,
MartinJohnson 0:404dae88af71 38 0x40, 0x40,
MartinJohnson 0:404dae88af71 39 0x7f, 0xC0,
MartinJohnson 0:404dae88af71 40 0x7f, 0xC0,
MartinJohnson 0:404dae88af71 41 0x7f, 0xC0,
MartinJohnson 0:404dae88af71 42 0x7f, 0xC0 };
MartinJohnson 0:404dae88af71 43
MartinJohnson 3:93e488fbb8a2 44 int colour=7;
MartinJohnson 0:404dae88af71 45
MartinJohnson 0:404dae88af71 46 u16 strLen(pu8 str) {
MartinJohnson 0:404dae88af71 47
MartinJohnson 0:404dae88af71 48 int i = 0;
MartinJohnson 0:404dae88af71 49
MartinJohnson 0:404dae88af71 50 while (*str != 0) {
MartinJohnson 0:404dae88af71 51 ++i;
MartinJohnson 0:404dae88af71 52 ++str;
MartinJohnson 0:404dae88af71 53 }
MartinJohnson 0:404dae88af71 54 return(i);
MartinJohnson 0:404dae88af71 55 }
MartinJohnson 0:404dae88af71 56
MartinJohnson 0:404dae88af71 57 // *****************************************************************************
MartinJohnson 0:404dae88af71 58 // Function gdiCopyRect(PGDI_RECT rc1, PGDI_RECT rc2)
MartinJohnson 0:404dae88af71 59 //
MartinJohnson 0:404dae88af71 60 // Copy rectangle rc2 to rc1
MartinJohnson 0:404dae88af71 61 //
MartinJohnson 0:404dae88af71 62 // parameters:
MartinJohnson 0:404dae88af71 63 // rc1 Destination rectangle
MartinJohnson 0:404dae88af71 64 // y Source rectangle
MartinJohnson 0:404dae88af71 65 //
MartinJohnson 0:404dae88af71 66 // return: none
MartinJohnson 0:404dae88af71 67 // *****************************************************************************
MartinJohnson 0:404dae88af71 68 void gdiCopyRect(PGDI_RECT rc1, PGDI_RECT rc2) {
MartinJohnson 0:404dae88af71 69
MartinJohnson 0:404dae88af71 70 rc1->x = rc2->x;
MartinJohnson 0:404dae88af71 71 rc1->y = rc2->y;
MartinJohnson 0:404dae88af71 72 rc1->w = rc2->w;
MartinJohnson 0:404dae88af71 73 rc1->h = rc2->h;
MartinJohnson 0:404dae88af71 74 }
MartinJohnson 0:404dae88af71 75
MartinJohnson 0:404dae88af71 76 // *****************************************************************************
MartinJohnson 0:404dae88af71 77 // Function gdiBitBlt(PGDI_RECT prc, i16 x, i16 y, i16 w, i16 h, pu8 bm, u16 rop)
MartinJohnson 0:404dae88af71 78 //
MartinJohnson 0:404dae88af71 79 // Bit Block Transfer funcion. This function uses the STM32 Bit-Banding mode
MartinJohnson 0:404dae88af71 80 // to simplify the complex BitBlt functionality.
MartinJohnson 0:404dae88af71 81 //
MartinJohnson 0:404dae88af71 82 // From Cortex STM32F10x Reference Manual (RM0008):
MartinJohnson 0:404dae88af71 83 // A mapping formula shows how to reference each word in the alias region to a
MartinJohnson 0:404dae88af71 84 // corresponding bit in the bit-band region. The mapping formula is:
MartinJohnson 3:93e488fbb8a2 85 // bit_word_addr = bit_band_base + (byte_offset x 32) + (bit_number � 4)
MartinJohnson 0:404dae88af71 86 // where:
MartinJohnson 0:404dae88af71 87 // bit_word_addr is the address of the word in the alias memory region that
MartinJohnson 0:404dae88af71 88 // maps to the targeted bit.
MartinJohnson 0:404dae88af71 89 // bit_band_base is the starting address of the alias region
MartinJohnson 0:404dae88af71 90 // byte_offset is the number of the byte in the bit-band region that contains
MartinJohnson 0:404dae88af71 91 // the targeted bit bit_number is the bit position (0-7) of the targeted bit.
MartinJohnson 0:404dae88af71 92 // Example:
MartinJohnson 0:404dae88af71 93 // The following example shows how to map bit 2 of the byte located at SRAM
MartinJohnson 0:404dae88af71 94 // address 0x20000300 in the alias region:
MartinJohnson 0:404dae88af71 95 // 0x22006008 = 0x22000000 + (0x300*32) + (2*4).
MartinJohnson 0:404dae88af71 96 // Writing to address 0x22006008 has the same effect as a read-modify-write
MartinJohnson 0:404dae88af71 97 // operation on bit 2 of the byte at SRAM address 0x20000300.
MartinJohnson 0:404dae88af71 98 // Reading address 0x22006008 returns the value (0x01 or 0x00) of bit 2 of
MartinJohnson 0:404dae88af71 99 // the byte at SRAM address 0x20000300 (0x01: bit set; 0x00: bit reset).
MartinJohnson 0:404dae88af71 100 //
MartinJohnson 0:404dae88af71 101 // For further reference see the Cortex M3 Technical Reference Manual
MartinJohnson 0:404dae88af71 102 //
MartinJohnson 0:404dae88af71 103 // Parameters:
MartinJohnson 0:404dae88af71 104 //
MartinJohnson 0:404dae88af71 105 // prc Clipping rectangle. All X/Y coordinates are inside "prc"
MartinJohnson 0:404dae88af71 106 // If "prc" is NULL, the coordinates will be the entire display
MartinJohnson 0:404dae88af71 107 // area
MartinJohnson 0:404dae88af71 108 // x Bitmap X start position
MartinJohnson 0:404dae88af71 109 // y Bitmap Y start position
MartinJohnson 0:404dae88af71 110 // w Bitmap width, in pixels
MartinJohnson 0:404dae88af71 111 // y Bitmap height, in pixels
MartinJohnson 0:404dae88af71 112 // bm Pointer to te bitmap start position
MartinJohnson 0:404dae88af71 113 // rop Raster operation. See GDI_ROP_xxx defines
MartinJohnson 0:404dae88af71 114 //
MartinJohnson 0:404dae88af71 115 // return none
MartinJohnson 0:404dae88af71 116 // ****************************************************************************
MartinJohnson 0:404dae88af71 117
MartinJohnson 0:404dae88af71 118 u8 leftmask[]={0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe};
MartinJohnson 0:404dae88af71 119 u8 rightmask[]={0x0,0x1,0x3,0x7,0xf,0x1f,0x3f,0x7f};
MartinJohnson 0:404dae88af71 120 CCM void gdiBitBlt(PGDI_RECT prc, i16 x, i16 y, i16 w, i16 h, pu8 bm, u16 rop) {
MartinJohnson 0:404dae88af71 121
MartinJohnson 0:404dae88af71 122 u16 i, xz, xb, xt;
MartinJohnson 0:404dae88af71 123 u32 wb; // Width in bytes
MartinJohnson 0:404dae88af71 124 u32 r,rr; // Start X position in bits (relative to x)
MartinJohnson 0:404dae88af71 125 u32 k;
MartinJohnson 0:404dae88af71 126 u32 d;
MartinJohnson 0:404dae88af71 127 u32 offs;
MartinJohnson 0:404dae88af71 128 u8 c;
MartinJohnson 0:404dae88af71 129 pu8 fbPtr; // Pointer to the Frame Buffer Bit-Band area
MartinJohnson 0:404dae88af71 130 pu8 fbBak;
MartinJohnson 0:404dae88af71 131 u8 fb1;
MartinJohnson 0:404dae88af71 132 u32 fb2;
MartinJohnson 0:404dae88af71 133 u32 rp;
MartinJohnson 0:404dae88af71 134 pu8 bmPtr; // Pointer to the bitmap bits
MartinJohnson 0:404dae88af71 135
MartinJohnson 0:404dae88af71 136 // Calculate clipping region
MartinJohnson 0:404dae88af71 137 if (prc != NULL) {
MartinJohnson 0:404dae88af71 138 x = prc->x + x;
MartinJohnson 0:404dae88af71 139 y = prc->y + y;
MartinJohnson 0:404dae88af71 140 }
MartinJohnson 0:404dae88af71 141
MartinJohnson 0:404dae88af71 142 // Get total bitmap width in bytes
MartinJohnson 0:404dae88af71 143
MartinJohnson 0:404dae88af71 144 wb = (u32) (w+7) >> 3;
MartinJohnson 0:404dae88af71 145 // if ((wb << 3) < (u32) w) ++wb;
MartinJohnson 0:404dae88af71 146
MartinJohnson 0:404dae88af71 147 // Get starting bit inside the first byte
MartinJohnson 0:404dae88af71 148
MartinJohnson 0:404dae88af71 149 r=x&7;
MartinJohnson 0:404dae88af71 150 xb=x>>3;
MartinJohnson 0:404dae88af71 151
MartinJohnson 0:404dae88af71 152 rr=7-((w+r)&7); // no of bits to mask at end of bitmap
MartinJohnson 0:404dae88af71 153
MartinJohnson 0:404dae88af71 154 // Clip X
MartinJohnson 0:404dae88af71 155
MartinJohnson 0:404dae88af71 156 if (prc == NULL) {
MartinJohnson 0:404dae88af71 157 if ((x + w) >= VID_PIXELS_X ) {
MartinJohnson 0:404dae88af71 158 xt = VID_PIXELS_X - x;
MartinJohnson 0:404dae88af71 159 } else {
MartinJohnson 0:404dae88af71 160 xt = w;
MartinJohnson 0:404dae88af71 161 }
MartinJohnson 0:404dae88af71 162 } else {
MartinJohnson 0:404dae88af71 163 if ((x + w) >= (x + prc->w)) {
MartinJohnson 0:404dae88af71 164 xt = prc->w - x;
MartinJohnson 0:404dae88af71 165 } else {
MartinJohnson 0:404dae88af71 166 xt = w;
MartinJohnson 0:404dae88af71 167 }
MartinJohnson 0:404dae88af71 168 }
MartinJohnson 0:404dae88af71 169
MartinJohnson 0:404dae88af71 170 // Draw bits
MartinJohnson 3:93e488fbb8a2 171 /*
MartinJohnson 0:404dae88af71 172 for (i = 0; i < h; i++) {
MartinJohnson 0:404dae88af71 173 u8 lst=0,d;
MartinJohnson 0:404dae88af71 174 // Clip Y
MartinJohnson 0:404dae88af71 175 bmPtr = bm + ((u32) i * wb);
MartinJohnson 0:404dae88af71 176 if ((i + y) > (VID_VSIZE - 1)) return;
MartinJohnson 0:404dae88af71 177 if(rop==GDI_ROP_COPY) {
MartinJohnson 0:404dae88af71 178 c = *bmPtr++;
MartinJohnson 0:404dae88af71 179 if((w+r)<8)
MartinJohnson 0:404dae88af71 180 fb[i+y][xb] = (fb[i+y][xb]&(leftmask[r]|rightmask[rr]))|(c>>r);
MartinJohnson 0:404dae88af71 181 else {
MartinJohnson 0:404dae88af71 182 fb[i+y][xb] = (fb[i+y][xb]&leftmask[r])|(c>>r);
MartinJohnson 0:404dae88af71 183 lst=(c<<(8-r));
MartinJohnson 0:404dae88af71 184 for(xz=1;xz<wb;xz++) {
MartinJohnson 0:404dae88af71 185 c = *bmPtr++;
MartinJohnson 0:404dae88af71 186 fb[i+y][xz+xb]=((c>>r)|lst);
MartinJohnson 0:404dae88af71 187 lst=(c<<(8-r));
MartinJohnson 0:404dae88af71 188 }
MartinJohnson 0:404dae88af71 189 fb[i+y][xb+wb] = (fb[i+y][xb+wb]&rightmask[rr])|lst;
MartinJohnson 0:404dae88af71 190 }
MartinJohnson 0:404dae88af71 191 }
MartinJohnson 0:404dae88af71 192 if(rop==GDI_ROP_XOR) {
MartinJohnson 0:404dae88af71 193 for(xz=0;xz<wb;xz++) {
MartinJohnson 0:404dae88af71 194 c = *bmPtr++;
MartinJohnson 0:404dae88af71 195 fb[i+y][xz+xb]^=((c>>r)|lst);
MartinJohnson 0:404dae88af71 196 lst=(c<<(8-r));
MartinJohnson 0:404dae88af71 197 }
MartinJohnson 0:404dae88af71 198 if((w+r)>8)
MartinJohnson 0:404dae88af71 199 fb[i+y][xz+xb]^=lst;
MartinJohnson 0:404dae88af71 200 }
MartinJohnson 0:404dae88af71 201 if(rop==GDI_ROP_OR) {
MartinJohnson 0:404dae88af71 202 for(xz=0;xz<wb;xz++) {
MartinJohnson 0:404dae88af71 203 c = *bmPtr++;
MartinJohnson 0:404dae88af71 204 fb[i+y][xz+xb]|=((c>>r)|lst);
MartinJohnson 0:404dae88af71 205 lst=(c<<(8-r));
MartinJohnson 0:404dae88af71 206 }
MartinJohnson 0:404dae88af71 207 if((w+r)>8)
MartinJohnson 0:404dae88af71 208 fb[i+y][xz+xb]|=lst;
MartinJohnson 0:404dae88af71 209 }
MartinJohnson 0:404dae88af71 210
MartinJohnson 3:93e488fbb8a2 211 */
MartinJohnson 0:404dae88af71 212
MartinJohnson 0:404dae88af71 213 // Get offset to frame buffer in bit-banding mode
MartinJohnson 3:93e488fbb8a2 214 for (i = 0; i < h; i++) {
MartinJohnson 3:93e488fbb8a2 215 u8 lst=0,d;
MartinJohnson 3:93e488fbb8a2 216
MartinJohnson 0:404dae88af71 217 /*
MartinJohnson 0:404dae88af71 218 offs = (((u32) x >> 3)) + ((u32) (y + i) * VID_HSIZE_R);
MartinJohnson 0:404dae88af71 219 k = (u32) (&fb - 0x20000000);
MartinJohnson 0:404dae88af71 220 k += offs;
MartinJohnson 0:404dae88af71 221 fbPtr = (pu8) (0x22000000 + (k * 32) + ((7 - r) * 4));
MartinJohnson 0:404dae88af71 222 fbBak = (pu8) (0x22000000 + (k * 32) + 28);
MartinJohnson 3:93e488fbb8a2 223 */
MartinJohnson 3:93e488fbb8a2 224
MartinJohnson 3:93e488fbb8a2 225 fbPtr = fb[y+i]+x;
MartinJohnson 3:93e488fbb8a2 226 if(fbPtr > fb[VID_VSIZE-1]) return;
MartinJohnson 0:404dae88af71 227
MartinJohnson 0:404dae88af71 228 // Get offset to bitmap bits
MartinJohnson 0:404dae88af71 229
MartinJohnson 0:404dae88af71 230 bmPtr = bm + ((u32) i * wb);
MartinJohnson 0:404dae88af71 231 xz = w;
MartinJohnson 0:404dae88af71 232
MartinJohnson 0:404dae88af71 233 xb = 0;
MartinJohnson 0:404dae88af71 234 for (xz = 0; xz < xt; xz++) {
MartinJohnson 3:93e488fbb8a2 235 //fb1 = ((u32) fbPtr) & 0x000000E0;
MartinJohnson 3:93e488fbb8a2 236 if(x+xz > VID_HSIZE) break;
MartinJohnson 0:404dae88af71 237 if (xb++ == 0) {
MartinJohnson 0:404dae88af71 238 c = *bmPtr;
MartinJohnson 0:404dae88af71 239 ++bmPtr;
MartinJohnson 0:404dae88af71 240 }
MartinJohnson 0:404dae88af71 241 xb &= 0x07;
MartinJohnson 3:93e488fbb8a2 242 (c & 0x80) ? (rp = colour<<4) : (rp = 0);
MartinJohnson 0:404dae88af71 243 switch(rop) {
MartinJohnson 3:93e488fbb8a2 244 case GDI_ROP_COPY: *fbPtr |= rp; break;
MartinJohnson 0:404dae88af71 245 case GDI_ROP_XOR: *fbPtr ^= rp; break;
MartinJohnson 0:404dae88af71 246 case GDI_ROP_AND: *fbPtr &= rp; break;
MartinJohnson 0:404dae88af71 247 case GDI_ROP_OR: *fbPtr |= rp; break;
MartinJohnson 0:404dae88af71 248 }
MartinJohnson 3:93e488fbb8a2 249 fbPtr += 1;
MartinJohnson 3:93e488fbb8a2 250
MartinJohnson 3:93e488fbb8a2 251 /*
MartinJohnson 0:404dae88af71 252 fb2 = ((u32) fbPtr) & 0x000000E0;
MartinJohnson 0:404dae88af71 253 if (fb1 != fb2) {
MartinJohnson 0:404dae88af71 254 fbPtr = fbBak + 32;
MartinJohnson 0:404dae88af71 255 fbBak = fbPtr;
MartinJohnson 0:404dae88af71 256 }
MartinJohnson 3:93e488fbb8a2 257 */
MartinJohnson 0:404dae88af71 258 c <<= 1;
MartinJohnson 0:404dae88af71 259 }
MartinJohnson 3:93e488fbb8a2 260
MartinJohnson 0:404dae88af71 261 }
MartinJohnson 0:404dae88af71 262 }
MartinJohnson 3:93e488fbb8a2 263 void gdiSetColour(int c) {
MartinJohnson 3:93e488fbb8a2 264 colour=c;
MartinJohnson 3:93e488fbb8a2 265 }
MartinJohnson 0:404dae88af71 266 // *****************************************************************************
MartinJohnson 0:404dae88af71 267 // Function gdiPoint(PGDI_RECT rc, u16 x, u16 y)
MartinJohnson 0:404dae88af71 268 //
MartinJohnson 0:404dae88af71 269 // Show a point in x/y position using the current graphical mode stored in
MartinJohnson 0:404dae88af71 270 // grMode variable
MartinJohnson 0:404dae88af71 271 //
MartinJohnson 0:404dae88af71 272 // parameters:
MartinJohnson 0:404dae88af71 273 // x X position
MartinJohnson 0:404dae88af71 274 // y Y position
MartinJohnson 0:404dae88af71 275 // rop Raster operation. See GDI_ROP_xxx defines
MartinJohnson 0:404dae88af71 276 //
MartinJohnson 0:404dae88af71 277 // return: none
MartinJohnson 0:404dae88af71 278 // *****************************************************************************
MartinJohnson 3:93e488fbb8a2 279 __attribute__ ((section ("ccmram"))) void gdiPoint(PGDI_RECT rc, u16 x, u16 y, u16 rop) {
MartinJohnson 0:404dae88af71 280
MartinJohnson 0:404dae88af71 281 u16 w, r;
MartinJohnson 0:404dae88af71 282 u8 m;
MartinJohnson 0:404dae88af71 283
MartinJohnson 0:404dae88af71 284 // Test for point outside display area
MartinJohnson 0:404dae88af71 285
MartinJohnson 0:404dae88af71 286 if (x >= VID_PIXELS_X || y >= VID_PIXELS_Y) return;
MartinJohnson 0:404dae88af71 287
MartinJohnson 3:93e488fbb8a2 288 w = x;// >> 3;
MartinJohnson 0:404dae88af71 289 r = x - (w << 3);
MartinJohnson 0:404dae88af71 290
MartinJohnson 0:404dae88af71 291 // Prepare mask
MartinJohnson 0:404dae88af71 292
MartinJohnson 3:93e488fbb8a2 293 m = colour<<4;//(0x80 >> r);
MartinJohnson 0:404dae88af71 294
MartinJohnson 0:404dae88af71 295 switch(rop) {
MartinJohnson 0:404dae88af71 296 case GDI_ROP_COPY: fb[y][w] |= m;
MartinJohnson 0:404dae88af71 297 break;
MartinJohnson 0:404dae88af71 298 case GDI_ROP_XOR: fb[y][w] ^= m;
MartinJohnson 0:404dae88af71 299 break;
MartinJohnson 0:404dae88af71 300 case GDI_ROP_AND: fb[y][w] &= m;
MartinJohnson 0:404dae88af71 301 break;
MartinJohnson 0:404dae88af71 302 }
MartinJohnson 0:404dae88af71 303 }
MartinJohnson 0:404dae88af71 304
MartinJohnson 0:404dae88af71 305 //*****************************************************************************
MartinJohnson 0:404dae88af71 306 // Function gdiLine(i16 x1, i16 y1, i16 x2, i16 y2, u16 rop)
MartinJohnson 0:404dae88af71 307 //
MartinJohnson 0:404dae88af71 308 // Draw line using Bresenham algorithm
MartinJohnson 0:404dae88af71 309 //
MartinJohnson 0:404dae88af71 310 // This function was taken from the book:
MartinJohnson 0:404dae88af71 311 // Interactive Computer Graphics, A top-down approach with OpenGL
MartinJohnson 0:404dae88af71 312 // written by Emeritus Edward Angel
MartinJohnson 0:404dae88af71 313 //
MartinJohnson 0:404dae88af71 314 // parameters:
MartinJohnson 0:404dae88af71 315 // prc Clipping rectangle
MartinJohnson 0:404dae88af71 316 // x1 X start position
MartinJohnson 0:404dae88af71 317 // y1 Y start position
MartinJohnson 0:404dae88af71 318 // x2 X end position
MartinJohnson 0:404dae88af71 319 // y2 Y end position
MartinJohnson 0:404dae88af71 320 // rop Raster operation. See GDI_ROP_xxx defines
MartinJohnson 0:404dae88af71 321 //
MartinJohnson 0:404dae88af71 322 // return none
MartinJohnson 0:404dae88af71 323 //*****************************************************************************
MartinJohnson 0:404dae88af71 324 void gdiLine(PGDI_RECT prc, i16 x1, i16 y1, i16 x2, i16 y2, u16 rop) {
MartinJohnson 0:404dae88af71 325
MartinJohnson 0:404dae88af71 326 i16 dx, dy, i, e;
MartinJohnson 0:404dae88af71 327 i16 incx, incy, inc1, inc2;
MartinJohnson 0:404dae88af71 328 i16 x, y;
MartinJohnson 0:404dae88af71 329
MartinJohnson 0:404dae88af71 330 dx = x2 - x1;
MartinJohnson 0:404dae88af71 331 dy = y2 - y1;
MartinJohnson 0:404dae88af71 332
MartinJohnson 0:404dae88af71 333 if(dx < 0) dx = -dx;
MartinJohnson 0:404dae88af71 334 if(dy < 0) dy = -dy;
MartinJohnson 0:404dae88af71 335 incx = 1;
MartinJohnson 0:404dae88af71 336 if(x2 < x1) incx = -1;
MartinJohnson 0:404dae88af71 337 incy = 1;
MartinJohnson 0:404dae88af71 338 if(y2 < y1) incy = -1;
MartinJohnson 0:404dae88af71 339 x=x1;
MartinJohnson 0:404dae88af71 340 y=y1;
MartinJohnson 0:404dae88af71 341
MartinJohnson 0:404dae88af71 342 if (dx > dy) {
MartinJohnson 0:404dae88af71 343 gdiPoint(prc, x, y, rop);
MartinJohnson 0:404dae88af71 344 e = 2*dy - dx;
MartinJohnson 0:404dae88af71 345 inc1 = 2 * ( dy -dx);
MartinJohnson 0:404dae88af71 346 inc2 = 2 * dy;
MartinJohnson 0:404dae88af71 347 for (i = 0; i < dx; i++) {
MartinJohnson 0:404dae88af71 348 if (e >= 0) {
MartinJohnson 0:404dae88af71 349 y += incy;
MartinJohnson 0:404dae88af71 350 e += inc1;
MartinJohnson 0:404dae88af71 351 }
MartinJohnson 0:404dae88af71 352 else {
MartinJohnson 0:404dae88af71 353 e += inc2;
MartinJohnson 0:404dae88af71 354 }
MartinJohnson 0:404dae88af71 355 x += incx;
MartinJohnson 0:404dae88af71 356 gdiPoint(prc, x, y, rop);
MartinJohnson 0:404dae88af71 357 }
MartinJohnson 0:404dae88af71 358 } else {
MartinJohnson 0:404dae88af71 359 gdiPoint(prc, x, y, rop);
MartinJohnson 0:404dae88af71 360 e = 2 * dx - dy;
MartinJohnson 0:404dae88af71 361 inc1 = 2 * (dx - dy);
MartinJohnson 0:404dae88af71 362 inc2 = 2 * dx;
MartinJohnson 0:404dae88af71 363 for(i = 0; i < dy; i++) {
MartinJohnson 0:404dae88af71 364 if (e >= 0) {
MartinJohnson 0:404dae88af71 365 x += incx;
MartinJohnson 0:404dae88af71 366 e += inc1;
MartinJohnson 0:404dae88af71 367 } else {
MartinJohnson 0:404dae88af71 368 e += inc2;
MartinJohnson 0:404dae88af71 369 }
MartinJohnson 0:404dae88af71 370 y += incy;
MartinJohnson 0:404dae88af71 371 gdiPoint(prc, x, y, rop);
MartinJohnson 0:404dae88af71 372 }
MartinJohnson 0:404dae88af71 373 }
MartinJohnson 0:404dae88af71 374 }
MartinJohnson 0:404dae88af71 375
MartinJohnson 0:404dae88af71 376 //*****************************************************************************
MartinJohnson 0:404dae88af71 377 // Function gdiRectangle(i16 x1, i16 y1, i16 x2, i16 y2, u16 rop)
MartinJohnson 0:404dae88af71 378 //
MartinJohnson 0:404dae88af71 379 // Draw rectangle
MartinJohnson 0:404dae88af71 380 //
MartinJohnson 0:404dae88af71 381 // parameters:
MartinJohnson 0:404dae88af71 382 // x1 X start position
MartinJohnson 0:404dae88af71 383 // y1 Y start position
MartinJohnson 0:404dae88af71 384 // x2 X end position
MartinJohnson 0:404dae88af71 385 // y2 Y end position
MartinJohnson 0:404dae88af71 386 // rop Raster operation. See GDI_ROP_xxx defines
MartinJohnson 0:404dae88af71 387 //
MartinJohnson 0:404dae88af71 388 // return none
MartinJohnson 0:404dae88af71 389 //*****************************************************************************
MartinJohnson 0:404dae88af71 390 void gdiRectangle(i16 x0, i16 y0, i16 x1, i16 y1, u16 rop) {
MartinJohnson 0:404dae88af71 391
MartinJohnson 0:404dae88af71 392 gdiLine(NULL,x0,y0,x1,y0,rop);
MartinJohnson 0:404dae88af71 393 gdiLine(NULL,x0,y1,x1,y1,rop);
MartinJohnson 0:404dae88af71 394 gdiLine(NULL,x0,y0,x0,y1,rop);
MartinJohnson 0:404dae88af71 395 gdiLine(NULL,x1,y0,x1,y1,rop);
MartinJohnson 0:404dae88af71 396 }
MartinJohnson 0:404dae88af71 397
MartinJohnson 0:404dae88af71 398 //*****************************************************************************
MartinJohnson 0:404dae88af71 399 // Function gdiRectangleEx(PGDI_RECT rc, u16 rop)
MartinJohnson 0:404dae88af71 400 //
MartinJohnson 0:404dae88af71 401 // Draw rectangle
MartinJohnson 0:404dae88af71 402 //
MartinJohnson 0:404dae88af71 403 // parameters:
MartinJohnson 0:404dae88af71 404 // rc Struct containing the rectangle parameters
MartinJohnson 0:404dae88af71 405 // rop Raster operation. See GDI_ROP_xxx defines
MartinJohnson 0:404dae88af71 406 //
MartinJohnson 0:404dae88af71 407 // return none
MartinJohnson 0:404dae88af71 408 //*****************************************************************************
MartinJohnson 0:404dae88af71 409 void gdiRectangleEx(PGDI_RECT rc, u16 rop) {
MartinJohnson 0:404dae88af71 410
MartinJohnson 0:404dae88af71 411 gdiRectangle(rc->x, rc->y, rc->x + rc->w, rc->y + rc->h,rop);
MartinJohnson 0:404dae88af71 412 }
MartinJohnson 0:404dae88af71 413
MartinJohnson 0:404dae88af71 414 //*****************************************************************************
MartinJohnson 0:404dae88af71 415 // Function gdiCircle(i16 x, i16 y, i16 r, u16 rop)
MartinJohnson 0:404dae88af71 416 //
MartinJohnson 0:404dae88af71 417 // Draw circle. This function uses the integer-precision math
MartinJohnson 0:404dae88af71 418 //
MartinJohnson 0:404dae88af71 419 // parameters:
MartinJohnson 0:404dae88af71 420 // x Circle center X position
MartinJohnson 0:404dae88af71 421 // y Circle center Y position
MartinJohnson 0:404dae88af71 422 // r Radius
MartinJohnson 0:404dae88af71 423 // rop Raster operation. See GDI_ROP_xxx defines
MartinJohnson 0:404dae88af71 424 //
MartinJohnson 0:404dae88af71 425 // return none
MartinJohnson 0:404dae88af71 426 //*****************************************************************************
MartinJohnson 0:404dae88af71 427 void gdiCircle(u16 x, u16 y, u16 r, u16 rop) {
MartinJohnson 0:404dae88af71 428
MartinJohnson 0:404dae88af71 429 i32 x1, y1;
MartinJohnson 0:404dae88af71 430 u16 a;
MartinJohnson 0:404dae88af71 431
MartinJohnson 3:93e488fbb8a2 432 int div;
MartinJohnson 3:93e488fbb8a2 433 div=360/(16*r);
MartinJohnson 3:93e488fbb8a2 434 if(div==0) div=1;
MartinJohnson 3:93e488fbb8a2 435 for (a = 0; a < 360; a+=div) {
MartinJohnson 0:404dae88af71 436 x1 = r * mthCos(a);
MartinJohnson 0:404dae88af71 437 y1 = r * mthSin(a);
MartinJohnson 0:404dae88af71 438 gdiPoint(NULL, (x1 / 10000) + x,(y1 / 10000) + y,rop);
MartinJohnson 0:404dae88af71 439 }
MartinJohnson 0:404dae88af71 440 }
MartinJohnson 0:404dae88af71 441
MartinJohnson 3:93e488fbb8a2 442 void gdiFilledCircle(u16 x, u16 y, u16 r, u16 rop) {
MartinJohnson 3:93e488fbb8a2 443
MartinJohnson 3:93e488fbb8a2 444 i32 x1, y1;
MartinJohnson 3:93e488fbb8a2 445 u16 a;
MartinJohnson 3:93e488fbb8a2 446
MartinJohnson 3:93e488fbb8a2 447 int div;
MartinJohnson 3:93e488fbb8a2 448 div=360/(16*r);
MartinJohnson 3:93e488fbb8a2 449 if(div==0) div=1;
MartinJohnson 3:93e488fbb8a2 450 for (a = 0; a < 100; a+=div) {
MartinJohnson 3:93e488fbb8a2 451 x1 = r * mthCos(a);
MartinJohnson 3:93e488fbb8a2 452 y1 = r * mthSin(a);
MartinJohnson 3:93e488fbb8a2 453 gdiLine(NULL, (x1 / 10000) + x,(y1 / 10000) + y,-(x1 / 10000) + x,(y1 / 10000) + y,rop);
MartinJohnson 3:93e488fbb8a2 454 gdiLine(NULL, (x1 / 10000) + x,-(y1 / 10000) + y,-(x1 / 10000) + x,-(y1 / 10000) + y,rop);
MartinJohnson 3:93e488fbb8a2 455 }
MartinJohnson 3:93e488fbb8a2 456 }
MartinJohnson 0:404dae88af71 457 //*****************************************************************************
MartinJohnson 0:404dae88af71 458 // Function gdiDrawText(PGDI_RECT prc, pu8 ptext, u16 style, u16 rop)
MartinJohnson 0:404dae88af71 459 //
MartinJohnson 0:404dae88af71 460 // Draw text inside rectangle
MartinJohnson 0:404dae88af71 461 //
MartinJohnson 0:404dae88af71 462 // parameters:
MartinJohnson 0:404dae88af71 463 // prc Pointer to clipping rectangle
MartinJohnson 0:404dae88af71 464 // ptext Pointer to text
MartinJohnson 0:404dae88af71 465 // style Text style (see GDI_WINCAPTION_xx defines)
MartinJohnson 0:404dae88af71 466 // rop Raster operation. See GDI_ROP_xxx defines
MartinJohnson 0:404dae88af71 467 //
MartinJohnson 0:404dae88af71 468 // return none
MartinJohnson 0:404dae88af71 469 //*****************************************************************************
MartinJohnson 0:404dae88af71 470 CCM void gdiDrawText(PGDI_RECT prc, pu8 ptext, u16 style, u16 rop) {
MartinJohnson 0:404dae88af71 471
MartinJohnson 0:404dae88af71 472 u16 l, i, pos, xp;
MartinJohnson 0:404dae88af71 473 u8 c;
MartinJohnson 0:404dae88af71 474 pu8 ptx;
MartinJohnson 0:404dae88af71 475
MartinJohnson 0:404dae88af71 476 l = strLen(ptext) * GDI_SYSFONT_WIDTH;
MartinJohnson 0:404dae88af71 477 switch(style) {
MartinJohnson 0:404dae88af71 478 case GDI_WINCAPTION_RIGHT: if (l < prc->w) {
MartinJohnson 0:404dae88af71 479 prc->x += (prc->w - l);
MartinJohnson 0:404dae88af71 480 }
MartinJohnson 0:404dae88af71 481 break;
MartinJohnson 0:404dae88af71 482 case GDI_WINCAPTION_CENTER: if (l < prc->w) {
MartinJohnson 0:404dae88af71 483 prc->x += ((prc->w - l) / 2);
MartinJohnson 0:404dae88af71 484 }
MartinJohnson 0:404dae88af71 485 break;
MartinJohnson 0:404dae88af71 486 }
MartinJohnson 0:404dae88af71 487 l = strLen(ptext);
MartinJohnson 0:404dae88af71 488 xp = 1;//prc->x;
MartinJohnson 0:404dae88af71 489 for (i = 0; i < l; i++) {
MartinJohnson 0:404dae88af71 490 c = *(ptext++);
MartinJohnson 0:404dae88af71 491 if (c >= GDI_SYSFONT_OFFSET) {
MartinJohnson 0:404dae88af71 492 pos = (u16) (c - GDI_SYSFONT_OFFSET) * GDI_SYSFONT_BYTEWIDTH * GDI_SYSFONT_HEIGHT;
MartinJohnson 0:404dae88af71 493 ptx = ((pu8) gdiSystemFont) + pos;
MartinJohnson 0:404dae88af71 494 gdiBitBlt(prc, xp, 0, GDI_SYSFONT_WIDTH, GDI_SYSFONT_HEIGHT, ptx, rop);
MartinJohnson 0:404dae88af71 495 xp += GDI_SYSFONT_WIDTH;
MartinJohnson 0:404dae88af71 496 if (xp >= ((prc->x + prc->w) - GDI_SYSFONT_WIDTH)) return;
MartinJohnson 0:404dae88af71 497 }
MartinJohnson 0:404dae88af71 498 }
MartinJohnson 0:404dae88af71 499 }
MartinJohnson 0:404dae88af71 500
MartinJohnson 0:404dae88af71 501 //*****************************************************************************
MartinJohnson 0:404dae88af71 502 // Function gdiDrawTextEx(i16 x, i16 y, pu8 ptext, u16 rop)
MartinJohnson 0:404dae88af71 503 //
MartinJohnson 0:404dae88af71 504 // Draw text in X/Y position using system font.
MartinJohnson 0:404dae88af71 505 //
MartinJohnson 0:404dae88af71 506 // parameters:
MartinJohnson 0:404dae88af71 507 // x X start position
MartinJohnson 0:404dae88af71 508 // y Y start position
MartinJohnson 0:404dae88af71 509 // ptext Pointer to text
MartinJohnson 0:404dae88af71 510 // rop Raster operation. See GDI_ROP_xxx defines
MartinJohnson 0:404dae88af71 511 //
MartinJohnson 0:404dae88af71 512 // return none
MartinJohnson 0:404dae88af71 513 //*****************************************************************************
MartinJohnson 0:404dae88af71 514 void gdiDrawTextEx(i16 x, i16 y, pu8 ptext, u16 rop) {
MartinJohnson 0:404dae88af71 515
MartinJohnson 0:404dae88af71 516 u16 l, i, pos, xp;
MartinJohnson 0:404dae88af71 517 u8 c;
MartinJohnson 0:404dae88af71 518 pu8 ptx;
MartinJohnson 0:404dae88af71 519
MartinJohnson 0:404dae88af71 520 l = strLen(ptext);
MartinJohnson 0:404dae88af71 521 xp = x;
MartinJohnson 0:404dae88af71 522 for (i = 0; i < l; i++) {
MartinJohnson 0:404dae88af71 523 c = *(ptext++);
MartinJohnson 0:404dae88af71 524 if (c >= GDI_SYSFONT_OFFSET) {
MartinJohnson 0:404dae88af71 525 pos = (u16) (c - GDI_SYSFONT_OFFSET) * GDI_SYSFONT_BYTEWIDTH * GDI_SYSFONT_HEIGHT;
MartinJohnson 0:404dae88af71 526 ptx = ((pu8) gdiSystemFont) + pos;
MartinJohnson 0:404dae88af71 527 gdiBitBlt(NULL, xp, y, GDI_SYSFONT_WIDTH, GDI_SYSFONT_HEIGHT, ptx, rop);
MartinJohnson 0:404dae88af71 528 xp += GDI_SYSFONT_WIDTH;
MartinJohnson 0:404dae88af71 529 if (xp >= VID_PIXELS_X) return;
MartinJohnson 0:404dae88af71 530 }
MartinJohnson 0:404dae88af71 531 }
MartinJohnson 0:404dae88af71 532 }
MartinJohnson 0:404dae88af71 533 //*****************************************************************************
MartinJohnson 0:404dae88af71 534 // Function gdiDrawWindow(PGDI_WINDOW pwin)
MartinJohnson 0:404dae88af71 535 //
MartinJohnson 0:404dae88af71 536 // Draw window
MartinJohnson 0:404dae88af71 537 //
MartinJohnson 0:404dae88af71 538 // parameters:
MartinJohnson 0:404dae88af71 539 // pwin Pointer to windows struct
MartinJohnson 0:404dae88af71 540 //
MartinJohnson 0:404dae88af71 541 // return none
MartinJohnson 0:404dae88af71 542 //*****************************************************************************
MartinJohnson 0:404dae88af71 543 void gdiDrawWindow(PGDI_WINDOW pwin) {
MartinJohnson 0:404dae88af71 544
MartinJohnson 0:404dae88af71 545 i16 i;
MartinJohnson 0:404dae88af71 546 GDI_RECT rc, rt;
MartinJohnson 0:404dae88af71 547
MartinJohnson 0:404dae88af71 548 gdiCopyRect(&rc,&pwin->rc);
MartinJohnson 0:404dae88af71 549 if (pwin->style & GDI_WINCAPTION) {
MartinJohnson 0:404dae88af71 550 gdiCopyRect(&rt,&pwin->rc);
MartinJohnson 0:404dae88af71 551 rt.h = rt.y + 11;
MartinJohnson 0:404dae88af71 552 rt.x += 2;
MartinJohnson 0:404dae88af71 553 rt.y += 1;
MartinJohnson 0:404dae88af71 554 rc.h += 10;
MartinJohnson 0:404dae88af71 555 for (i = 0; i < 11; i++) {
MartinJohnson 0:404dae88af71 556 gdiLine(NULL,rc.x, rc.y + i, rc.x + rc.w, rc.y + i, GDI_ROP_COPY);
MartinJohnson 0:404dae88af71 557 }
MartinJohnson 0:404dae88af71 558 if (pwin->style & GDI_WINCLOSEICON) {
MartinJohnson 0:404dae88af71 559 gdiBitBlt(&rc, rc.w - 9, 1, 10, 9, (pu8) gdiCloseBm, GDI_ROP_COPY);
MartinJohnson 0:404dae88af71 560 rt.w -= 11;
MartinJohnson 0:404dae88af71 561 } else {
MartinJohnson 0:404dae88af71 562 rt.w -= 1;
MartinJohnson 0:404dae88af71 563 }
MartinJohnson 0:404dae88af71 564 gdiDrawText(&rt,pwin->caption,pwin->style & GDI_WINCAPTION_MASK, GDI_ROP_XOR);
MartinJohnson 0:404dae88af71 565 }
MartinJohnson 0:404dae88af71 566 gdiRectangleEx(&rc,GDI_ROP_COPY);
MartinJohnson 0:404dae88af71 567 }
MartinJohnson 0:404dae88af71 568
MartinJohnson 0:404dae88af71 569