How can I display an image with citro2d ?

  • Thread starter Thread starter iZma
  • Start date Start date
  • Views Views 3,168
  • Replies Replies 9

iZma

Member
Newcomer
Joined
Jul 31, 2022
Messages
7
Reaction score
0
Trophies
0
Age
25
Location
Albacete
XP
63
Country
Spain
I've been searching ways to display an image for a game I'm doing for the 3ds, I tried to do it with citro2d but I'm really confused.
Any suggestions?
 
It gives me an error or even the sprites don't even show up. I also tried code from a guy but the code gives me an error too.
 
Hi, I tested the spritesheet example but my image doesn't appear on screen, this is my code:
C++:
#include <citro2d.h>

#include <time.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define TOP_SCREEN_WIDTH  400
#define BOTTOM_SCREEN_WIDTH 320
#define BOTH_SCREEN_HEIGHT 240

class Game {

    public:

    u32 kDown = hidKeysDown();

    touchPosition plyrLsr;

    C2D_Sprite plyrSprite;

    C2D_SpriteSheet plyrSprSheet;
    
    int plyrLsrLastX = 0,
        plyrLsrLastY = 0,
        plyrSprW = 32, plyrSprH = 32; // Delete plyrSprW/H if not used <<!!!>> more than once!

    u32 clrClear = C2D_Color32(0xB4, 0xB4, 0xB4, 0xB4);
    u32 clrWall = C2D_Color32(0x64, 0x64, 0x64, 0xFF);
    u32 clrPlyr = C2D_Color32(0xFF, 0x0, 0x0, 0xFF);

    void initSpr() {
        C2D_SpriteFromSheet(&plyrSprite, plyrSprSheet, 0);
        C2D_SpriteSetCenter(&plyrSprite, plyrSprW/2, plyrSprH/2);
        C2D_SpriteSetPos(&plyrSprite, TOP_SCREEN_WIDTH/2, BOTH_SCREEN_HEIGHT/2);
    }

    void top_screen() {
        C2D_DrawSprite(&plyrSprite);
    }

    void bottom_screen() {
        if (plyrLsrLastX == 0 && plyrLsrLastY == 0) {
            plyrLsr.px = BOTTOM_SCREEN_WIDTH/2;
            plyrLsr.py = BOTH_SCREEN_HEIGHT/2;
        }

        if (plyrLsr.px == 0 && plyrLsr.py == 0) {
                plyrLsr.px = plyrLsrLastX;
                plyrLsr.py = plyrLsrLastY;
        }

        plyrLsrLastX = plyrLsr.px;
        plyrLsrLastY = plyrLsr.py;

        C2D_DrawLine(BOTTOM_SCREEN_WIDTH/2, BOTH_SCREEN_HEIGHT/2, clrWall, plyrLsr.px, plyrLsr.py, clrWall, 2 , 0);
    }

    void run() {
        // Init libs
        romfsInit();
        gfxInitDefault();
        C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
        C2D_Init(C2D_DEFAULT_MAX_OBJECTS);
        C2D_Prepare();
        
        // Create screens
        C3D_RenderTarget* top = C2D_CreateScreenTarget(GFX_TOP, GFX_RIGHT);
        C3D_RenderTarget* bottom = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_RIGHT);

        plyrSprSheet = C2D_SpriteSheetLoad("romfs:/gfx/sprites.t3x");
        if (!plyrSprSheet) svcBreak(USERBREAK_PANIC);

        initSpr();

        while (aptMainLoop())
        {
            hidScanInput();

            // Respond to user input
            if (kDown & KEY_START) break; // break in order to return to hbmenu

            hidTouchRead(&plyrLsr);

            // For now we wont use this (for now...)
            /*printf("\x1b[1;1HImportant info:");
            printf("\x1b[2;1HCPU:     %6.2f%%\x1b[K", C3D_GetProcessingTime()*6.0f);
            printf("\x1b[3;1HGPU:     %6.2f%%\x1b[K", C3D_GetDrawingTime()*6.0f);
            printf("\x1b[4;1HCmdBuf:  %6.2f%%\x1b[K", C3D_GetCmdBufUsage()*100.0f); */

            // Render the scene
            //--------------------------------
            C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
            C2D_TargetClear(top, clrClear);
            C2D_SceneBegin(top);

            top_screen();

            C2D_TargetClear(bottom, clrClear);
            C2D_SceneBegin(bottom);
            //--------------------------------

            bottom_screen();

            // End teh frame
            C3D_FrameEnd(0);
        }
    }
};

//---------------------------------------------------------------------------------
int main() {
//---------------------------------------------------------------------------------

    // Game run

    Game game;

    game.run();

    // Deinit libs
    C2D_SpriteSheetFree(game.plyrSprSheet);
    C2D_Fini();
    C3D_Fini();
    gfxExit();
    romfsExit();
    return 0;
}
 
Hi, I tested the spritesheet example but my image doesn't appear on screen, this is my code:
The centering should be 0.0f~1.0f I think, so doing 32/2 is probably placing it way off screen, if working at all. This seems to work:
Diff:
diff --git a/test/source/main.cpp b/test/source/main.cpp
index 4f952a6..18fff93 100644
--- a/test/source/main.cpp
+++ b/test/source/main.cpp
@@ -32,7 +32,7 @@ class Game {
 
     void initSpr() {
         C2D_SpriteFromSheet(&plyrSprite, plyrSprSheet, 0);
-        C2D_SpriteSetCenter(&plyrSprite, plyrSprW/2, plyrSprH/2);
+        C2D_SpriteSetCenter(&plyrSprite, 0.5f, 0.5f);
         C2D_SpriteSetPos(&plyrSprite, TOP_SCREEN_WIDTH/2, BOTH_SCREEN_HEIGHT/2);
     }
 
  • Like
Reactions: iZma
Thanks! Also, for some reason when I press start it doesn't exit to the hombrew.
You're never updating the variable again in the while loop, you need to add kDown = hidKeysDown(); to the loop, probably right after the scan keys.
 
  • Like
Reactions: iZma

Site & Scene News

Popular threads in this forum