ps2toys/sdl2/main.c

85 lines
2.0 KiB
C
Raw Permalink Normal View History

2024-09-23 07:42:01 +00:00
#include <kernel.h>
#include <sifrpc.h>
#include <loadfile.h>
#include <sys/stat.h>
2024-09-23 07:42:01 +00:00
#include "SDL2/SDL.h"
int printf(const char *format, ...);
char * ps2elf_storage;
int loadModule(char * module) {
printf("Loading module '%s' ...\n",module);
int rtn = SifLoadModule(module,0,NULL);
if (rtn < 0) {
printf("Could not load module '%s': %d\n",module,rtn);
}
return rtn;
}
2024-09-23 07:42:01 +00:00
int main(int argc, char **argv)
{
// Just some debugging
printf("Arguments:\n");
for (int i=0; i < argc; i++) {
printf("* %d: %s\n",i,argv[i]);
}
printf("\n");
// Getting the device from elf path
char * sep = strchr(argv[0], ':');
if (sep == NULL) {
printf("ERROR: Could not find storage device of elf!\n");
return 1;
} else {
SifInitRpc(0);
2024-09-23 07:42:01 +00:00
size_t size = sep - argv[0];
ps2elf_storage = malloc(size + 1);
memcpy(ps2elf_storage,argv[0],size);
ps2elf_storage[size] = 0;
printf("* Detected storage device: '%s'\n",ps2elf_storage);
if (strcmp(ps2elf_storage,"mc0") || strcmp(ps2elf_storage,"mc1")) {
if (
(loadModule("rom:SIO2MAN") < 0) ||
(loadModule("rom:MCMAN") < 0)
) {
return 1;
}
}
2024-09-23 07:42:01 +00:00
}
// Initializing SDL2
printf("[SDL2] Initializing ...\n");
SDL_Init(SDL_INIT_VIDEO);
printf("[SDL2] Creating window ...\n");
int width = 640;
int height = 480;
SDL_Window * window = SDL_CreateWindow("game",
0,0,
width,height,
0
);
printf("[SDL2] Grabbing surface ...\n");
SDL_Surface * surface = SDL_GetWindowSurface(window);
Uint16 * pixels = surface->pixels;
Uint32 tracker = 0;
// Draw-loop
printf("[SDL2] Entering draw-loop ...\n");
int x; int y;
while (1) {
// Just doing some fun animated pattern
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
pixels[(((y * width) + x) + tracker) % (width * height)] = (tracker + (x ^ y)) + y;
}
}
tracker = tracker + 3;
SDL_UpdateWindowSurface(window);
printf("\r"); fflush(stdout); // printf seems to fire an interrupt, which allows ps2link to reset the console, so we keep sending an "empty" debug message.
}
return 0;
}