-
Notifications
You must be signed in to change notification settings - Fork 0
/
myLib.c
152 lines (131 loc) · 3.08 KB
/
myLib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdlib.h>
#include <string.h>
#include "myLib.h"
u16 *videoBuffer = (u16 *)0x6000000;
void setPixel(int r, int c, u16 color)
{
*(videoBuffer + (240*r) + c) = color;
}
void drawRect(int row, int col, int width, int height, u16 color)
{
int r;
for(r=0; r<height; r++)
{
DMA[3].src = &color;
DMA[3].dst = &videoBuffer[OFFSET(row+r, col, 240)];
DMA[3].cnt = width | DMA_SOURCE_FIXED | DMA_ON;
}
}
void drawHollowRect(int r, int c, int width, int height, u16
color)
{
int i;
for (i = 0; i < width; i++) {
setPixel(r, c + i, color);
setPixel(r + height - 1, c + i, color);
}
for (i = 0; i < height; i++) {
setPixel(r + i, c, color);
setPixel(r + i, c + width - 1, color);
}
}
void drawChar(int row, int col, char ch, unsigned short color)
{
int r,c;
for(r=0; r<8; r++)
{
for(c=0; c<6; c++)
{
if(fontdata_6x8[r*6 + c +ch*48])
{
setPixel(row+r, col+c, color);
}
}
}
}
void drawString(int row, int col, char *str, unsigned short color)
{
while(*str)
{
drawChar(row, col, *str++, color);
col += 6;
}
}
void drawMovingPiece(int r, int c, u16 color)
{
int i, j, k = 0;
videoBuffer[240*r + c] = CYAN;
for (i = 4; i > 0; i--)
{
for (j = 1; j < i; j++) {
if(k != 0)
{
if((c+j) < 239) {
videoBuffer[240*(r+k) + (c+j)] = color;
videoBuffer[240*(r-k) + (c+j)] = color;
}
if((c-j) > 0) {
videoBuffer[240*(r-k) + (c-j)] = color;
videoBuffer[240*(r+k) + (c-j)] = color;
}
}
}
k++;
}
}
void drawChoice(Choice choice, u16 boxcolor, u16 strcolor)
{
drawRect(choice.row - 1, choice.col - 1, STRWIDTH(strlen(choice.str)) + 2, STRHEIGHT + 2, boxcolor);
drawString(choice.row, choice.col, choice.str, strcolor);
}
void drawChooser(Choice choice)
{
drawHollowRect(choice.row - 1, choice.col - 1, STRWIDTH(strlen(choice.str)) + 2, STRHEIGHT + 2, YELLOW);
}
void fillScreen(volatile unsigned short color)
{
DMA[3].src = &color;
DMA[3].dst = videoBuffer;
DMA[3].cnt = (160*240) | DMA_SOURCE_FIXED | DMA_ON;
}
void fillBackground(const unsigned short arr[])
{
DMA[3].src = arr;
DMA[3].dst = videoBuffer;
DMA[3].cnt = (160*240) | DMA_ON;
}
void drawImage3(int r, int c, int width, int height, const u16* image)
{
for (int i = 0; i < height; i++)
{
DMA[3].src = &image[i*width];
DMA[3].dst = &videoBuffer[(i+r)*240 + c];
DMA[3].cnt = width | DMA_ON;
}
}
void drawBorderedRectangle(int row, int col, int height, int width, volatile u16 borderedColor, volatile u16 fillColor)
{
int r;
for(r=0; r<height; r++)
{
volatile u16 color;
if(r==0 || r == (height -1))
{
color = borderedColor;
}
else
{
color = fillColor;
}
DMA[3].src = &color;
DMA[3].dst = videoBuffer + (240*(row+r) + col);
DMA[3].cnt = DMA_ON | DMA_SOURCE_FIXED | width;
videoBuffer[240*(row+r) + col] = borderedColor;
videoBuffer[240*(row+r) + width + col] = borderedColor;
}
}
void waitForVblank()
{
while(SCANLINECOUNTER > 160);
while(SCANLINECOUNTER < 160);
}