|
| 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 | +} |
0 commit comments