cursor.c
11677 bytes ยท Original / download
#include "cursor.h"
#include "utils/glutil.h"
#include "utils/logger.h"
#include <vitaGL.h>
#include <psp2/kernel/clib.h>
#include <psp2/kernel/threadmgr.h>
#include <stdlib.h>
#define STBI_ONLY_PNG
#define STB_IMAGE_IMPLEMENTATION
#include "../lib/vitagl/samples/skybox_env_map/stb_image.h"
#define CURSOR_POINTER_PATH "app0:/assets/cursor/pointer.png"
#define CURSOR_VIEW_PATH "app0:/assets/cursor/view.png"
#define CURSOR_PLUS_PATH "app0:/assets/cursor/plus.png"
#define LOAD_BACKGROUND_PATH "app0:/assets/load/Load.png"
#define LOAD_BLOOD_PATH "app0:/assets/load/sangue.png"
#define CURSOR_SIZE 96
#define LOAD_BAR_X 140
#define LOAD_BAR_Y 290
#define LOAD_BAR_WIDTH 680
#define LOAD_BAR_HEIGHT 7
typedef struct OverlayVertex {
float x, y;
float u, v;
} OverlayVertex;
typedef struct CursorBitmap {
unsigned char *pixels;
int width;
int height;
} CursorBitmap;
static CursorBitmap pointer_bitmap;
static CursorBitmap view_bitmap;
static CursorBitmap plus_bitmap;
static GLuint loading_program;
static GLuint loading_background;
static GLuint loading_blood;
static GLint loading_position;
static GLint loading_texcoord;
static GLint loading_sampler;
static int loading_ready;
static int decode_cursor(const char *path, CursorBitmap *bitmap) {
int channels = 0;
bitmap->pixels = stbi_load(path, &bitmap->width, &bitmap->height, &channels, 4);
if (!bitmap->pixels) {
l_error("CURSOR decode failed path=%s reason=%s", path,
stbi_failure_reason() ? stbi_failure_reason() : "unknown");
return 0;
}
l_info("CURSOR decoded path=%s size=%dx%d channels=%d", path,
bitmap->width, bitmap->height, channels);
return 1;
}
static GLuint compile_shader(GLenum type, const char *source) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
return shader;
}
static GLuint upload_texture(const char *path) {
int width = 0, height = 0, channels = 0;
unsigned char *pixels = stbi_load(path, &width, &height, &channels, 4);
if (!pixels) {
l_error("LOADER decode failed path=%s reason=%s", path,
stbi_failure_reason() ? stbi_failure_reason() : "unknown");
return 0;
}
while (glGetError() != GL_NO_ERROR) {}
GLuint texture = 0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, pixels);
GLenum error = glGetError();
stbi_image_free(pixels);
if (error != GL_NO_ERROR) {
l_error("LOADER texture upload failed path=%s error=0x%04x", path, error);
if (texture) glDeleteTextures(1, &texture);
return 0;
}
l_info("LOADER texture loaded path=%s id=%u size=%dx%d", path, texture,
width, height);
return texture;
}
static int init_loading_renderer(void) {
static const char *vertex_source =
"attribute vec2 a_position;\n"
"attribute vec2 a_texcoord;\n"
"varying vec2 v_texcoord;\n"
"void main(){v_texcoord=a_texcoord;gl_Position=vec4(a_position,0.0,1.0);}\n";
static const char *fragment_source =
"precision mediump float;\n"
"varying vec2 v_texcoord;\n"
"uniform sampler2D u_texture;\n"
"void main(){gl_FragColor=texture2D(u_texture,v_texcoord);}\n";
GLuint vertex = compile_shader(GL_VERTEX_SHADER, vertex_source);
GLuint fragment = compile_shader(GL_FRAGMENT_SHADER, fragment_source);
loading_program = glCreateProgram();
glAttachShader(loading_program, vertex);
glAttachShader(loading_program, fragment);
glBindAttribLocation(loading_program, 0, "a_position");
glBindAttribLocation(loading_program, 1, "a_texcoord");
glLinkProgram(loading_program);
GLint linked = GL_FALSE;
glGetProgramiv(loading_program, GL_LINK_STATUS, &linked);
glDeleteShader(vertex);
glDeleteShader(fragment);
if (!linked) {
char info[512] = {0};
GLsizei length = 0;
glGetProgramInfoLog(loading_program, sizeof(info) - 1, &length, info);
l_error("LOADER program link failed log=%s", length ? info : "<empty>");
return 0;
}
loading_position = glGetAttribLocation(loading_program, "a_position");
loading_texcoord = glGetAttribLocation(loading_program, "a_texcoord");
loading_sampler = glGetUniformLocation(loading_program, "u_texture");
loading_background = upload_texture(LOAD_BACKGROUND_PATH);
loading_blood = upload_texture(LOAD_BLOOD_PATH);
loading_ready = loading_background && loading_blood;
l_info("LOADER overlay initialized program=%u background=%u blood=%u ready=%d",
loading_program, loading_background, loading_blood, loading_ready);
return loading_ready;
}
int cursor_init(void) {
int pointer_ok = decode_cursor(CURSOR_POINTER_PATH, &pointer_bitmap);
int view_ok = decode_cursor(CURSOR_VIEW_PATH, &view_bitmap);
int plus_ok = decode_cursor(CURSOR_PLUS_PATH, &plus_bitmap);
int loading_ok = init_loading_renderer();
l_info("OVERLAY initialized pointer=%d view=%d plus=%d loading=%d", pointer_ok,
view_ok, plus_ok, loading_ok);
return pointer_ok && view_ok && plus_ok && loading_ok;
}
static void draw_fullscreen(GLuint texture, int alpha_blend) {
static const OverlayVertex vertices[6] = {
{-1.0f, 1.0f, 0.0f, 0.0f}, {-1.0f, -1.0f, 0.0f, 1.0f},
{ 1.0f, -1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, 0.0f, 0.0f},
{ 1.0f, -1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f, 0.0f}
};
glDisable(GL_SCISSOR_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
if (alpha_blend) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} else {
glDisable(GL_BLEND);
}
glViewport(0, 0, 960, 544);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUseProgram(loading_program);
glUniform1i(loading_sampler, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray((GLuint)loading_position);
glEnableVertexAttribArray((GLuint)loading_texcoord);
glVertexAttribPointer((GLuint)loading_position, 2, GL_FLOAT, GL_FALSE,
sizeof(OverlayVertex), &vertices[0].x);
glVertexAttribPointer((GLuint)loading_texcoord, 2, GL_FLOAT, GL_FALSE,
sizeof(OverlayVertex), &vertices[0].u);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
static void draw_blood_bar(float progress) {
int width = (int)(LOAD_BAR_WIDTH * progress);
if (width < 0) width = 0;
if (width > LOAD_BAR_WIDTH) width = LOAD_BAR_WIDTH;
glDisable(GL_BLEND);
glEnable(GL_SCISSOR_TEST);
glScissor(LOAD_BAR_X - 1, LOAD_BAR_Y - 1,
LOAD_BAR_WIDTH + 2, LOAD_BAR_HEIGHT + 2);
glClearColor(0.08f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
if (width > 0) {
glScissor(LOAD_BAR_X, LOAD_BAR_Y, width, LOAD_BAR_HEIGHT);
glClearColor(0.38f, 0.0f, 0.015f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
glDisable(GL_SCISSOR_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void cursor_loading_draw(float progress) {
if (!loading_ready) {
int width = (int)(LOAD_BAR_WIDTH * progress);
if (width < 0) width = 0;
if (width > LOAD_BAR_WIDTH) width = LOAD_BAR_WIDTH;
glDisable(GL_SCISSOR_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_SCISSOR_TEST);
glScissor(LOAD_BAR_X, LOAD_BAR_Y, width, LOAD_BAR_HEIGHT);
glClearColor(0.38f, 0.0f, 0.015f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_SCISSOR_TEST);
gl_swap();
return;
}
draw_fullscreen(loading_background, 0);
draw_blood_bar(progress);
gl_swap();
}
void cursor_loading_complete(void) {
if (loading_ready) {
draw_fullscreen(loading_background, 0);
draw_fullscreen(loading_blood, 1);
draw_blood_bar(1.0f);
gl_swap();
l_info("LOADER complete frame displayed; holding 1 second");
sceKernelDelayThread(1000000);
glDisableVertexAttribArray((GLuint)loading_position);
glDisableVertexAttribArray((GLuint)loading_texcoord);
glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisable(GL_SCISSOR_TEST);
glDisable(GL_BLEND);
glDeleteTextures(1, &loading_background);
glDeleteTextures(1, &loading_blood);
glDeleteProgram(loading_program);
loading_background = 0;
loading_blood = 0;
loading_program = 0;
loading_ready = 0;
} else {
cursor_loading_draw(1.0f);
sceKernelDelayThread(1000000);
}
while (glGetError() != GL_NO_ERROR) {}
l_info("LOADER overlay released; transitioning to game");
}
static unsigned int quantized_color(const unsigned char *pixel) {
unsigned int r = pixel[0] >> 4;
unsigned int g = pixel[1] >> 4;
unsigned int b = pixel[2] >> 4;
return (r << 8) | (g << 4) | b;
}
void cursor_draw(float x, float y, int state) {
CursorBitmap *bitmap = &pointer_bitmap;
if (state == CURSOR_STATE_CLICK && plus_bitmap.pixels)
bitmap = &plus_bitmap;
else if (state == CURSOR_STATE_HOVER && view_bitmap.pixels)
bitmap = &view_bitmap;
if (!bitmap->pixels || bitmap->width <= 0 || bitmap->height <= 0) return;
int left = (int)x - CURSOR_SIZE / 2;
int top = (int)y - CURSOR_SIZE / 2;
glEnable(GL_SCISSOR_TEST);
for (int dy = 0; dy < CURSOR_SIZE; ++dy) {
int source_y = dy * bitmap->height / CURSOR_SIZE;
int screen_y = top + dy;
if (screen_y < 0 || screen_y >= 544) continue;
int dx = 0;
while (dx < CURSOR_SIZE) {
int source_x = dx * bitmap->width / CURSOR_SIZE;
const unsigned char *pixel =
&bitmap->pixels[(source_y * bitmap->width + source_x) * 4];
if (pixel[3] < 48) {
++dx;
continue;
}
unsigned int color = quantized_color(pixel);
int run = 1;
while (dx + run < CURSOR_SIZE) {
int next_x = (dx + run) * bitmap->width / CURSOR_SIZE;
const unsigned char *next =
&bitmap->pixels[(source_y * bitmap->width + next_x) * 4];
if (next[3] < 48 || quantized_color(next) != color) break;
++run;
}
int screen_x = left + dx;
int clipped_run = run;
if (screen_x < 0) {
clipped_run += screen_x;
screen_x = 0;
}
if (screen_x + clipped_run > 960)
clipped_run = 960 - screen_x;
if (clipped_run > 0) {
float r = (float)((color >> 8) & 0xF) / 15.0f;
float g = (float)((color >> 4) & 0xF) / 15.0f;
float b = (float)(color & 0xF) / 15.0f;
glScissor(screen_x, 543 - screen_y, clipped_run, 1);
glClearColor(r, g, b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
dx += run;
}
}
glDisable(GL_SCISSOR_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}