Skip to content

Commit b04e897

Browse files
authored
Merge pull request #965 from PS2HomeDeveloper/add-a-usb-mouse-sample
Add a USB mouse sample
2 parents 9af5f7d + c4df587 commit b04e897

14 files changed

Lines changed: 1547 additions & 0 deletions

File tree

‎ee/rpc/mouse/samples/Makefile‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# _____ ___ ____ ___ ____
2+
# ____| | ____| | | |____|
3+
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
4+
#-----------------------------------------------------------------------
5+
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
6+
# Licenced under Academic Free License version 2.0
7+
# Review ps2sdk README & LICENSE files for further details.
8+
9+
SAMPLE_DIR = rpc/mouse
10+
11+
include $(PS2SDKSRC)/Defs.make
12+
include $(PS2SDKSRC)/samples/Rules.samples
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# _____ ___ ____ ___ ____
2+
# ____| | ____| | | |____|
3+
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
4+
#-----------------------------------------------------------------------
5+
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
6+
# Licenced under Academic Free License version 2.0
7+
# Review ps2sdk README & LICENSE files for further details.
8+
9+
EE_BIN = mouse.elf
10+
EE_OBJS = mouse.o gfx.o square.o menu.o settings.o
11+
EE_LIBS = -lmouse -lpatches -ldebug -ldraw -lgraph -lmath3d -lpacket -ldma
12+
13+
IRX_FILES += usbd.irx ps2mouse.irx
14+
EE_OBJS += $(IRX_FILES:.irx=_irx.o)
15+
16+
all: cursor.c $(EE_BIN)
17+
18+
cursor.c: cursor.raw
19+
bin2c cursor.raw cursor.c cursor
20+
21+
clean:
22+
rm -f $(EE_BIN) $(EE_OBJS) cursor.c *_irx.c
23+
24+
%_irx.c:
25+
bin2c $(PS2SDK)/iop/irx/$*.irx $@ $*_irx
26+
27+
run: $(EE_BIN)
28+
ps2client execee host:$(EE_BIN)
29+
30+
reset:
31+
ps2client reset
32+
33+
include $(PS2SDK)/samples/Makefile.pref
34+
include $(PS2SDK)/samples/Makefile.eeglobal

‎ee/rpc/mouse/samples/cursor.raw‎

1 KB
Binary file not shown.

‎ee/rpc/mouse/samples/gfx.c‎

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
/*
2+
# _____ ___ ____ ___ ____
3+
# ____| | ____| | | |____|
4+
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5+
#-----------------------------------------------------------------------
6+
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7+
# Licenced under Academic Free License version 2.0
8+
# Review ps2sdk README & LICENSE files for further details.
9+
#
10+
# USB mouse sample: the screen. Draws rectangles, the pointer and text.
11+
*/
12+
13+
#include <stdio.h>
14+
#include <stdarg.h>
15+
#include <string.h>
16+
#include <tamtypes.h>
17+
#include <debug.h>
18+
#include <gs_psm.h>
19+
#include <dma.h>
20+
#include <packet.h>
21+
#include <graph.h>
22+
#include <draw.h>
23+
#include <draw2d.h>
24+
25+
#include "gfx.h"
26+
27+
/*
28+
* cursor.raw is the pointer image: 16x16 pixels, 4 bytes per pixel (R, G, B, A).
29+
* The alpha goes from 0 (transparent) to 0x80 (opaque), as the GS expects.
30+
*/
31+
#include "cursor.c"
32+
33+
extern unsigned char cursor[];
34+
35+
/* A rectangle uses 3 quadwords and the pointer 4. */
36+
#define PACKET_QWORDS 200
37+
38+
int screen_height;
39+
40+
static packet_t *packet;
41+
static qword_t *current;
42+
43+
/* Send the packet to the GS and wait until everything in it is drawn. */
44+
static void send_packet(qword_t *end)
45+
{
46+
dma_wait_fast();
47+
dma_channel_send_normal(DMA_CHANNEL_GIF, packet->data, end - packet->data, 0, 0);
48+
draw_wait_finish();
49+
}
50+
51+
/*
52+
* The text is drawn by libdebug in a 32 bit framebuffer at the start of the VRAM, so the
53+
* rectangles and the pointer are drawn in the same framebuffer.
54+
*
55+
* libdebug shows only the top half of that framebuffer (each row is stretched to two lines
56+
* of the TV), so everything drawn in the bottom half stays out of the screen.
57+
* graph_initialize() sets the display again, to show the whole framebuffer.
58+
*/
59+
void gfx_init(void)
60+
{
61+
framebuffer_t frame;
62+
zbuffer_t z;
63+
texbuffer_t texbuf;
64+
clutbuffer_t clut;
65+
lod_t lod;
66+
qword_t *q;
67+
68+
screen_height = (graph_get_region() == GRAPH_MODE_PAL) ? 512 : 448;
69+
70+
dma_channel_initialize(DMA_CHANNEL_GIF, NULL, 0);
71+
dma_channel_fast_waits(DMA_CHANNEL_GIF);
72+
73+
frame.width = SCREEN_WIDTH;
74+
frame.height = screen_height;
75+
frame.mask = 0;
76+
frame.psm = GS_PSM_32;
77+
frame.address = graph_vram_allocate(frame.width, frame.height, frame.psm, GRAPH_ALIGN_PAGE);
78+
79+
graph_initialize(frame.address, frame.width, frame.height, frame.psm, 0, 0);
80+
81+
/* No depth test is needed for 2D drawing. */
82+
z.enable = DRAW_DISABLE;
83+
z.mask = 1;
84+
z.method = ZTEST_METHOD_ALLPASS;
85+
z.zsm = GS_ZBUF_32;
86+
z.address = graph_vram_allocate(frame.width, frame.height, z.zsm, GRAPH_ALIGN_PAGE);
87+
88+
/* The 16x16 pointer image is stored in a texture buffer that is 64 pixels wide. */
89+
texbuf.width = 64;
90+
texbuf.psm = GS_PSM_32;
91+
texbuf.address = graph_vram_allocate(64, CURSOR_SIZE, GS_PSM_32, GRAPH_ALIGN_BLOCK);
92+
93+
packet = packet_init(PACKET_QWORDS, PACKET_NORMAL);
94+
95+
q = packet->data;
96+
q = draw_setup_environment(q, 0, &frame, &z);
97+
q = draw_finish(q);
98+
send_packet(q);
99+
100+
q = packet->data;
101+
q = draw_texture_transfer(q, cursor, CURSOR_SIZE, CURSOR_SIZE, GS_PSM_32, texbuf.address, texbuf.width);
102+
q = draw_texture_flush(q);
103+
dma_wait_fast();
104+
dma_channel_send_chain(DMA_CHANNEL_GIF, packet->data, q - packet->data, 0, 0);
105+
dma_wait_fast();
106+
107+
lod.calculation = LOD_USE_K;
108+
lod.max_level = 0;
109+
lod.mag_filter = LOD_MAG_NEAREST;
110+
lod.min_filter = LOD_MIN_NEAREST;
111+
lod.l = 0;
112+
lod.k = 0;
113+
114+
texbuf.info.width = draw_log2(CURSOR_SIZE);
115+
texbuf.info.height = draw_log2(CURSOR_SIZE);
116+
texbuf.info.components = TEXTURE_COMPONENTS_RGBA;
117+
texbuf.info.function = TEXTURE_FUNCTION_MODULATE;
118+
119+
clut.storage_mode = CLUT_STORAGE_MODE1;
120+
clut.start = 0;
121+
clut.psm = 0;
122+
clut.load_method = CLUT_NO_LOAD;
123+
clut.address = 0;
124+
125+
q = packet->data;
126+
q = draw_texture_sampling(q, 0, &lod);
127+
q = draw_texturebuffer(q, 0, &texbuf, &clut);
128+
q = draw_finish(q);
129+
send_packet(q);
130+
131+
/* The pointer image has an alpha channel. */
132+
draw_enable_blending();
133+
}
134+
135+
void gfx_wait_vsync(void)
136+
{
137+
graph_wait_vsync();
138+
}
139+
140+
void gfx_begin(void)
141+
{
142+
current = packet->data;
143+
}
144+
145+
/* Draw what is in the packet, and start a new one. */
146+
static void flush(void)
147+
{
148+
current = draw_finish(current);
149+
send_packet(current);
150+
current = packet->data;
151+
}
152+
153+
void gfx_rect(int x, int y, int width, int height, u32 color)
154+
{
155+
rect_t rect;
156+
157+
if (current - packet->data > PACKET_QWORDS - 8)
158+
flush();
159+
160+
rect.v0.x = x;
161+
rect.v0.y = y;
162+
rect.v0.z = 0;
163+
rect.v1.x = x + width - 1;
164+
rect.v1.y = y + height - 1;
165+
rect.v1.z = 0;
166+
rect.color.r = color & 0xFF;
167+
rect.color.g = (color >> 8) & 0xFF;
168+
rect.color.b = (color >> 16) & 0xFF;
169+
rect.color.a = 0x80;
170+
rect.color.q = 1.0f;
171+
172+
current = draw_rect_filled(current, 0, &rect);
173+
}
174+
175+
void gfx_pointer(int x, int y)
176+
{
177+
texrect_t pointer;
178+
179+
if (current - packet->data > PACKET_QWORDS - 8)
180+
flush();
181+
182+
pointer.v0.x = x;
183+
pointer.v0.y = y;
184+
pointer.v0.z = 0;
185+
pointer.t0.u = 0.0f;
186+
pointer.t0.v = 0.0f;
187+
pointer.v1.x = x + CURSOR_SIZE - 1;
188+
pointer.v1.y = y + CURSOR_SIZE - 1;
189+
pointer.t1.u = CURSOR_SIZE;
190+
pointer.t1.v = CURSOR_SIZE;
191+
pointer.color.r = 0x80;
192+
pointer.color.g = 0x80;
193+
pointer.color.b = 0x80;
194+
pointer.color.a = 0x80;
195+
pointer.color.q = 1.0f;
196+
197+
current = draw_rect_textured(current, 0, &pointer);
198+
}
199+
200+
void gfx_end(void)
201+
{
202+
flush();
203+
}
204+
205+
static void draw_string(int x, int y, u32 color, u32 background, const char *text, int length)
206+
{
207+
int i;
208+
209+
scr_setbgcolor(background);
210+
for (i = 0; i < length; i++)
211+
scr_putchar(x + i * 8, y, color, text[i]);
212+
scr_setbgcolor(GFX_BLACK);
213+
}
214+
215+
void gfx_text(int x, int y, u32 color, u32 background, const char *format, ...)
216+
{
217+
char text[TEXT_COLUMNS + 1];
218+
va_list args;
219+
220+
va_start(args, format);
221+
vsnprintf(text, sizeof(text), format, args);
222+
va_end(args);
223+
224+
draw_string(x, y, color, background, text, strlen(text));
225+
}
226+
227+
/*
228+
* If the line was longer the last time, the rest of it is cleared, so the black background
229+
* of the letters is drawn only where it is needed.
230+
*/
231+
void gfx_print_line(int line, u32 background, const char *format, ...)
232+
{
233+
static int old_length[TEXT_LINES];
234+
char text[TEXT_COLUMNS + 1];
235+
va_list args;
236+
int length, i;
237+
238+
va_start(args, format);
239+
vsnprintf(text, sizeof(text), format, args);
240+
va_end(args);
241+
242+
length = strlen(text);
243+
for (i = length; i < old_length[line]; i++)
244+
text[i] = ' ';
245+
text[i] = '\0';
246+
247+
draw_string(TEXT_X, TEXT_Y + line * TEXT_LINE, GFX_WHITE, background, text, strlen(text));
248+
old_length[line] = length;
249+
}
250+
251+
void gfx_clear_lines(int first, int last)
252+
{
253+
int line;
254+
255+
for (line = first; line <= last; line++)
256+
gfx_print_line(line, GFX_BLACK, "%s", "");
257+
}

‎ee/rpc/mouse/samples/gfx.h‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
# _____ ___ ____ ___ ____
3+
# ____| | ____| | | |____|
4+
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5+
#-----------------------------------------------------------------------
6+
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7+
# Licenced under Academic Free License version 2.0
8+
# Review ps2sdk README & LICENSE files for further details.
9+
#
10+
# USB mouse sample: the screen. Draws rectangles, the pointer and text.
11+
*/
12+
13+
#ifndef GFX_H
14+
#define GFX_H
15+
16+
#include <tamtypes.h>
17+
18+
#define SCREEN_WIDTH 640
19+
#define CURSOR_SIZE 16
20+
21+
/* The text is drawn with the 8x8 font of libdebug, TEXT_LINE is the distance between two lines. */
22+
#define TEXT_X 8
23+
#define TEXT_Y 8
24+
#define TEXT_LINE 12
25+
#define TEXT_COLUMNS 44
26+
#define TEXT_LINES 36
27+
28+
/* Colors of the rectangles and of the text. */
29+
#define GFX_RGB(r, g, b) ((u32)(r) | ((u32)(g) << 8) | ((u32)(b) << 16))
30+
#define GFX_BLACK GFX_RGB(0x00, 0x00, 0x00)
31+
#define GFX_WHITE GFX_RGB(0xFF, 0xFF, 0xFF)
32+
#define GFX_RED GFX_RGB(0xFF, 0x00, 0x00)
33+
#define GFX_BLUE GFX_RGB(0x00, 0x00, 0xFF)
34+
#define GFX_SELECTED GFX_RGB(0x00, 0x60, 0xC0)
35+
#define GFX_HOVER GFX_RGB(0x40, 0x40, 0x40)
36+
#define GFX_GUIDE GFX_RGB(0x60, 0x60, 0x60)
37+
38+
/* The height of the screen is 448 on NTSC consoles and 512 on PAL consoles. Set by gfx_init(). */
39+
extern int screen_height;
40+
41+
void gfx_init(void);
42+
void gfx_wait_vsync(void);
43+
44+
/* Rectangles and the pointer are collected between gfx_begin() and gfx_end(), and drawn at once. */
45+
void gfx_begin(void);
46+
void gfx_rect(int x, int y, int width, int height, u32 color);
47+
void gfx_pointer(int x, int y);
48+
void gfx_end(void);
49+
50+
/* Text at any position of the screen. The letters have a background of the given color. */
51+
void gfx_text(int x, int y, u32 color, u32 background, const char *format, ...) __attribute__((format(printf, 5, 6)));
52+
53+
/* A line of text on the grid of TEXT_LINE. What was left of a longer line is cleared. */
54+
void gfx_print_line(int line, u32 background, const char *format, ...) __attribute__((format(printf, 3, 4)));
55+
void gfx_clear_lines(int first, int last);
56+
57+
#endif

0 commit comments

Comments
 (0)