-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.c
149 lines (122 loc) · 3.17 KB
/
draw.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
/*
* Copyright © 2015 Sergi Granell (xerpi)
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <psp2/display.h>
#include <psp2/gxm.h>
#include <psp2/kernel/sysmem.h>
#include "draw.h"
#define align_mem(addr, align) (((addr) + ((align) - 1)) & ~((align) - 1))
extern const unsigned char msx_font[];
static SceDisplayFrameBuf fb;
static SceUID fb_memuid;
static void *alloc_gpu_mem(uint32_t type, uint32_t size, uint32_t attribs, SceUID *uid)
{
void *mem = NULL;
if (type == SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW)
size = align_mem(size, 256 * 1024);
else
size = align_mem(size, 4 * 1024);
*uid = sceKernelAllocMemBlock("gxm", type, size, NULL);
if (sceKernelGetMemBlockBase(*uid, &mem) != 0)
return NULL;
if (sceGxmMapMemory(mem, size, attribs) != 0)
return NULL;
return mem;
}
void init_video()
{
SceGxmInitializeParams params;
params.flags = 0x0;
params.displayQueueMaxPendingCount = 0x1;
params.displayQueueCallback = 0x0;
params.displayQueueCallbackDataSize = 0x0;
params.parameterBufferSize = (16 * 1024 * 1024);
/* Initialize the GXM */
sceGxmInitialize(¶ms);
/* Setup framebuffers */
fb.size = sizeof(fb);
fb.pitch = SCREEN_W;
fb.pixelformat = SCE_DISPLAY_PIXELFORMAT_A8B8G8R8;
fb.width = SCREEN_W;
fb.height = SCREEN_H;
/* Allocate memory for the framebuffers */
fb.base = alloc_gpu_mem(SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW,
SCREEN_W * SCREEN_H * 4, SCE_GXM_MEMORY_ATTRIB_RW, &fb_memuid);
if (fb.base == NULL)
return;
sceDisplaySetFrameBuf(&fb, SCE_DISPLAY_SETBUF_NEXTFRAME);
}
void end_video()
{
sceGxmUnmapMemory(fb.base);
sceKernelFreeMemBlock(fb_memuid);
sceGxmTerminate();
}
void clear_screen()
{
memset(fb.base, 0x00, SCREEN_W*SCREEN_H*4);
}
void draw_pixel(uint32_t x, uint32_t y, uint32_t color)
{
((uint32_t *)fb.base)[x + y*fb.pitch] = color;
}
void draw_rectangle(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t color)
{
int i, j;
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
((uint32_t *)fb.base)[(x + j) + (y + i)*fb.pitch] = color;
}
}
}
void font_draw_char(int x, int y, uint32_t color, char c)
{
unsigned char *font = (unsigned char *)(msx_font + (c - (uint32_t)' ') * 8);
int i, j, pos_x, pos_y;
for (i = 0; i < 8; ++i) {
pos_y = y + i*2;
for (j = 0; j < 8; ++j) {
pos_x = x + j*2;
if ((*font & (128 >> j))) {
draw_pixel(pos_x + 0, pos_y + 0, color);
draw_pixel(pos_x + 1, pos_y + 0, color);
draw_pixel(pos_x + 0, pos_y + 1, color);
draw_pixel(pos_x + 1, pos_y + 1, color);
}
}
++font;
}
}
void font_draw_string(int x, int y, uint32_t color, const char *string)
{
if (string == NULL) return;
int startx = x;
const char *s = string;
while (*s) {
if (*s == '\n') {
x = startx;
y += 16;
} else if (*s == ' ') {
x += 16;
} else if(*s == '\t') {
x += 16*4;
} else {
font_draw_char(x, y, color, *s);
x += 16;
}
++s;
}
}
void font_draw_stringf(int x, int y, uint32_t color, const char *s, ...)
{
char buf[256];
va_list argptr;
va_start(argptr, s);
vsnprintf(buf, sizeof(buf), s, argptr);
va_end(argptr);
font_draw_string(x, y, color, buf);
}