-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.c
126 lines (107 loc) · 2.3 KB
/
console.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
/*
* Copyright © 2015 Sergi Granell (xerpi)
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
#include <psp2/kernel/threadmgr.h>
#include <psp2/kernel/error.h>
#include "console.h"
static int top_margin = 10;
static int cns_x = 0, cns_y = 0;
static uint32_t cns_color = WHITE;
static bool console_initialized = false;
static SceUID console_mtx;
void console_init()
{
if (console_initialized)
return;
console_reset();
console_mtx = sceKernelCreateMutex("console_mutex", 0, 0, NULL);
console_initialized = true;
}
void console_exit()
{
if (console_initialized)
sceKernelDeleteMutex(console_mtx);
}
void console_reset()
{
cns_x = 10;
cns_y = top_margin;
}
void console_putc(char c)
{
int mtx_err = sceKernelTryLockMutex(console_mtx, 1);
int last_y = cns_y;
if (c == '\r') {
draw_rectangle(0, cns_y, SCREEN_W, 20, BLACK);
cns_x = 10;
} else if (c == '\n') {
cns_y += 20;
cns_x = 10;
} else if (c == '\t') {
cns_x += 16*4;
} else if (c >= ' ' && c <= 126) {
font_draw_char(cns_x, cns_y, cns_color, c);
cns_x += 16;
}
if (cns_x >= (SCREEN_W-16)) {
cns_y += 20;
cns_x = 10;
}
if (cns_y >= (SCREEN_H-16)) {
cns_y = top_margin;
}
if (cns_y != last_y) {
draw_rectangle(0, cns_y, SCREEN_W, 20, BLACK);
if ((cns_y+20+16) < SCREEN_H) {
draw_rectangle(0, cns_y+20, SCREEN_W, 20, BLACK);
if ((cns_y+20+20+16) < SCREEN_H) {
draw_rectangle(0, cns_y+40, SCREEN_W, 20, BLACK);
}
}
}
if (mtx_err == SCE_KERNEL_OK)
sceKernelUnlockMutex(console_mtx, 1);
}
void console_print(const char *s)
{
int mtx_err = sceKernelTryLockMutex(console_mtx, 1);
while (*s) {
console_putc(*s);
s++;
}
if (mtx_err == SCE_KERNEL_OK)
sceKernelUnlockMutex(console_mtx, 1);
}
void console_printf(const char *s, ...)
{
unsigned int mtx_timeout = 0xFFFFFFFF;
sceKernelLockMutex(console_mtx, 1, &mtx_timeout);
char buf[256];
va_list argptr;
va_start(argptr, s);
vsnprintf(buf, sizeof(buf), s, argptr);
va_end(argptr);
console_print(buf);
sceKernelUnlockMutex(console_mtx, 1);
}
void console_set_color(uint32_t color)
{
cns_color = color;
}
int console_get_y()
{
return cns_y;
}
void console_set_y(int new_y)
{
cns_y = new_y;
draw_rectangle(0, cns_y, SCREEN_W, 20, BLACK);
}
void console_set_top_margin(int new_top_margin)
{
top_margin = new_top_margin;
}