Initialization of Graphics Rendering in C++

This snippet showcases the initialization of graphics rendering using the Gu library in C++. It sets up the draw buffer, viewport, depth buffer, and various rendering options to prepare for rendering graphics effectively.
 avatar
unknown
c_cpp
5 months ago
3.8 kB
5
Indexable
#include "../include/graphics_manager.hpp"

char list[0x20000] __attribute__((aligned(64)));

void initGu() {
    sceGuInit();

    sceGuStart(GU_DIRECT, list);

    sceGuDrawBuffer(GU_PSM_8888, (void*)0, BUFFER_WIDTH);
    sceGuDispBuffer(SCREEN_WIDTH, SCREEN_HEIGHT, (void*)0x88000, BUFFER_WIDTH);
    sceGuDepthBuffer((void*)0x110000, BUFFER_WIDTH);

    sceGuOffset(2048 - (SCREEN_WIDTH / 2), 2048 - (SCREEN_HEIGHT / 2));
    sceGuViewport(2048, 2048, SCREEN_WIDTH, SCREEN_HEIGHT);
    sceGuDepthRange(0, 65535);

    sceGuScissor(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    sceGuEnable(GU_SCISSOR_TEST);

    sceGuDisable(GU_DEPTH_TEST);

    sceGuShadeModel(GU_SMOOTH);

    sceGuDisable(GU_BLEND);

    sceGuEnable(GU_TEXTURE_2D);
    sceGuTexMode(GU_PSM_8888, 0, 0, 0);
    sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
    sceGuTexEnvColor(0x0);
    sceGuTexOffset(0.0f, 0.0f);
    sceGuTexScale(1.0f, 1.0f);
    sceGuTexWrap(GU_REPEAT, GU_REPEAT);
    sceGuTexFilter(GU_NEAREST, GU_NEAREST);

    sceGuFinish();
    sceGuSync(0, 0);
    sceGuDisplay(GU_TRUE);
}

void endGu() {
    sceGuDisplay(GU_FALSE);
    sceGuTerm();
}

void startFrame() {
    sceGuStart(GU_DIRECT, list);
    sceGuClearColor(0xFFFFFFFF);
    sceGuClear(GU_COLOR_BUFFER_BIT);
}

void endFrame() {
    sceGuFinish();
    sceGuSync(0, 0);
    sceDisplayWaitVblankStart();
    sceGuSwapBuffers();
}

void _drawRect(short x, short y, short w, short h, unsigned int color) {
    Vertex* vertices = (Vertex*)sceGuGetMemory(2 * sizeof(Vertex));

    vertices[0].x = x;
    vertices[0].y = y;

    vertices[1].x = x + w;
    vertices[1].y = y + h;

    sceGuColor(color);

    sceGuDrawArray(GU_SPRITES, GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, 0, vertices);
}

void _drawRectEx(short x, short y, short w, short h, unsigned int color, float angle) {
    Vertex* vertices = (Vertex*)sceGuGetMemory(2 * sizeof(Vertex));

    vertices[0].x = x;
    vertices[0].y = y;

    vertices[1].x = x + w;
    vertices[1].y = y + h;

    sceGuColor(color);

    sceGumPushMatrix();
    sceGumLoadIdentity();
    sceGumRotateZ(angle); 
    sceGuDrawArray(GU_SPRITES, GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, 0, vertices);
    sceGumPopMatrix();
}

void drawRect(Rect& rect, unsigned int color) {
    _drawRect(rect.x, rect.y, rect.w, rect.h, color);
}

void drawRect(Rect& rect, unsigned int color, float angle) {
    _drawRectEx(rect.x, rect.y, rect.w, rect.h, color, angle);
}

void drawString(const char* text, int x, int y, unsigned int color, int fw) {
    int len = (int)strlen(text);
    if(!len) {
        return;
    }

    typedef struct {
        float s, t;
        unsigned int c;
        float x, y, z;
    } VERT;

    VERT* v = (VERT*)sceGuGetMemory(sizeof(VERT) * 2 * len);

    int i;
    for(i = 0; i < len; i++) {
        unsigned char c = (unsigned char)text[i];
        if(c < 32) {
            c = 0;
        } else if(c >= 128) {
            c = 0;
        }

        int tx = (c & 0x0F) << 4;
        int ty = (c & 0xF0);

        VERT* v0 = &v[i*2+0];
        VERT* v1 = &v[i*2+1];
        
        v0->s = (float)(tx + (fw ? ((16 - fw) >> 1) : ((16 - fontwidthtab[c]) >> 1)));
        v0->t = (float)(ty);
        v0->c = color;
        v0->x = (float)(x);
        v0->y = (float)(y);
        v0->z = 0.0f;

        v1->s = (float)(tx + 16 - (fw ? ((16 - fw) >> 1) : ((16 - fontwidthtab[c]) >> 1)));
        v1->t = (float)(ty + 16);
        v1->c = color;
        v1->x = (float)(x + (fw ? fw : fontwidthtab[c]));
        v1->y = (float)(y + 16);
        v1->z = 0.0f;

        x += (fw ? fw : fontwidthtab[c]);
    }

    sceGumDrawArray(GU_SPRITES, 
        GU_TEXTURE_32BITF | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_2D,
        len * 2, 0, v
    );
}
Editor is loading...
Leave a Comment