-
Notifications
You must be signed in to change notification settings - Fork 13
/
graphics.c
executable file
·160 lines (134 loc) · 3.16 KB
/
graphics.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
153
154
155
156
157
158
159
160
#include "graphics.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#define SCE_DISPLAY_UPDATETIMING_NEXTVSYNC SCE_DISPLAY_SETBUF_NEXTFRAME
#include <psp2/display.h>
#include <psp2/kernel/sysmem.h>
#include <psp2/kernel/threadmgr.h>
enum {
SCREEN_WIDTH = 960,
SCREEN_HEIGHT = 544,
LINE_SIZE = 960,
FRAMEBUFFER_SIZE = 2 * 1024 * 1024,
FRAMEBUFFER_ALIGNMENT = 256 * 1024
};
typedef union
{
int rgba;
struct
{
char r;
char g;
char b;
char a;
} c;
} color_t;
extern u8 msx[];
void* g_vram_base;
static int gX = 0;
static int gY = 0;
static Color g_fg_color;
static Color g_bg_color;
static Color* getVramDisplayBuffer()
{
Color* vram = (Color*) g_vram_base;
return vram;
}
// #define LOG(args...) vita_logf (__FILE__, __LINE__, args) ///< Write a log entry
int g_log_mutex;
void psvDebugScreenInit() {
g_log_mutex = sceKernelCreateMutex("log_mutex", 0, 0, NULL);
SceKernelAllocMemBlockOpt opt = { 0 };
#ifdef VITASDK
opt.size = 4 * 5;
#else
opt.size = sizeof(opt);
#endif
opt.attr = 0x00000004;
opt.alignment = FRAMEBUFFER_ALIGNMENT;
SceUID displayblock = sceKernelAllocMemBlock("display", SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW, FRAMEBUFFER_SIZE, &opt);
printf("displayblock: 0x%08x", displayblock);
void *base;
sceKernelGetMemBlockBase(displayblock, &base);
// LOG("base: 0x%08x", base);
SceDisplayFrameBuf framebuf = { 0 };
framebuf.size = sizeof(framebuf);
framebuf.base = base;
framebuf.pitch = SCREEN_WIDTH;
framebuf.pixelformat = SCE_DISPLAY_PIXELFORMAT_A8B8G8R8;
framebuf.width = SCREEN_WIDTH;
framebuf.height = SCREEN_HEIGHT;
g_vram_base = base;
sceDisplaySetFrameBuf(&framebuf, SCE_DISPLAY_UPDATETIMING_NEXTVSYNC);
g_fg_color = 0xFFFFFFFF;
g_bg_color = 0x00000000;
}
void psvDebugScreenClear(int bg_color)
{
gX = gY = 0;
int i;
color_t *pixel = (color_t *)getVramDisplayBuffer();
for(i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) {
pixel->rgba = bg_color;
pixel++;
}
}
static void printTextScreen(const char * text)
{
int c, i, j, l;
u8 *font;
Color *vram_ptr;
Color *vram;
for (c = 0; c < strlen(text); c++) {
if (gX + 8 > SCREEN_WIDTH) {
gY += 8;
gX = 0;
}
if (gY + 8 > SCREEN_HEIGHT) {
gY = 0;
psvDebugScreenClear(g_bg_color);
}
char ch = text[c];
if (ch == '\n') {
gX = 0;
gY += 8;
continue;
} else if (ch == '\r') {
gX = 0;
continue;
}
vram = getVramDisplayBuffer() + gX + gY * LINE_SIZE;
font = &msx[ (int)ch * 8];
for (i = l = 0; i < 8; i++, l += 8, font++) {
vram_ptr = vram;
for (j = 0; j < 8; j++) {
if ((*font & (128 >> j))) *vram_ptr = g_fg_color;
else *vram_ptr = g_bg_color;
vram_ptr++;
}
vram += LINE_SIZE;
}
gX += 8;
}
}
void psvDebugScreenPrintf(const char *format, ...) {
char buf[512];
sceKernelLockMutex(g_log_mutex, 1, NULL);
va_list opt;
va_start(opt, format);
vsnprintf(buf, sizeof(buf), format, opt);
printTextScreen(buf);
va_end(opt);
sceKernelUnlockMutex(g_log_mutex, 1);
}
Color psvDebugScreenSetFgColor(Color color) {
Color prev_color = g_fg_color;
g_fg_color = color;
return prev_color;
}
Color psvDebugScreenSetBgColor(Color color) {
Color prev_color = g_bg_color;
g_bg_color = color;
return prev_color;
}