Homebrew app Want to use shapes on my 3DS with devkitpro.

Yukkuri_XP

New Member
OP
Newbie
Joined
Oct 14, 2022
Messages
4
Trophies
0
Location
ヰンドウズ
XP
65
Country
Japan
Hi.
I am making a program to draw shapes in devkitpro 3DS, but I am unable to build it due to an error.
I would appreciate it if you could tell me where I can correct the problem.
C++:
#include <3ds.h>
#include <stdio.h>
#include <string.h>

u8* pDispBuffer;
unsigned char PenColor[3];
unsigned char BrushColor[3];
unsigned int ClippingRect[4];

#define XYtoOfset(x,y) ((239-y)*3+(x*720))

//SelectFrame : set the frame pointer
// u8* pFrame : Pointer to framebuffer obtained by gfxGetFramebuffer
//
// Note: Always specify this before drawing

void SelectFrame(u8* pFrame) {
    if (pFrame) {
        pDispBuffer = pFrame;
    }
}

//SetClipping : set clipping area
// int x1,y1 : start point coordinates
// int x2,y2 : end point coordinates
void SetClipping(int x1, int y1, int x2, int y2) {
    ClippingRect[0] = x1;
    ClippingRect[1] = y1;
    ClippingRect[2] = x2;
    ClippingRect[3] = y2;
}
//SetPenColor          :pen color
// unsigned char r,g,b :color (RGB)
void SetPenColor(unsigned char r, unsigned char g, unsigned char b) {
    PenColor[2] = r;
    PenColor[1] = g;
    PenColor[0] = b;
}

//SetBrushColor        :brush color
// unsigned char r,g,b :color (RGB)
void SetBrushColor(unsigned char r, unsigned char g, unsigned char b) {
    BrushColor[2] = r;
    BrushColor[1] = g;
    BrushColor[0] = b;
}

//DrawDot : draw a point
// int x1,y1: coordinates
//
// Note: the color selected with the pen will be used.
void DrawDot(int x1, int y1) {
    if (pDispBuffer == NULL) return;
    if ((ClippingRect[0]<=x1)&&(ClippingRect[1]<=y1)&&(ClippingRect[2]>=x1)&&(ClippingRect[3]>=y1))
        memcpy(pDispBuffer + XYtoOfset(x1, y1), PenColor, sizeof(PenColor));
}

//DrawLine : draw a line
// int x1,y1 : start point coordinates
// int x2,y2 : end point coordinates
//
// Note: the color selected with the pen will be used.
void DrawLine(int x1, int y1, int x2, int y2) {
    DrawLineEx(x1, y1, x2, y2, (PenColor[2] << 16) | (PenColor[1] << 8) | PenColor[0]);
}
void DrawLineEx(int x1, int y1, int x2, int y2, int Color) {
    if (pDispBuffer == NULL) return;
    unsigned char brg[3] = { 0,0,0 };
    brg[2] = ((Color >> 16) & 0xFF);
    brg[1] = ((Color >> 8) & 0xFF);
    brg[0] = Color & 0xFF;
    int sp = 0, i = 0;
    double dSlope = 0.0, dSection = 0.0;
    if (x1 == x2) {
        if (y1 < y2) {
            sp = y1;
        } else {
            sp = y2;
        }
        for (i = sp; i < abs(y1 - y2)+sp; i++) {
            if ((ClippingRect[0]<=x1)&&(ClippingRect[1]<=i)&&(ClippingRect[2]>=x1)&&(ClippingRect[3]>=i))
                memcpy(pDispBuffer + XYtoOfset(x1, i), brg, sizeof(brg));
        }
        return;
    }
    dSlope = (double)(y1 - y2) / (double)(x1 - x2);
    dSection = (double)y1 - dSlope * x1;
    if (abs(x1 - x2) >= abs(y1 - y2)) {
        if (x1 < x2) {
            sp = x1;
        } else {
            sp = x2;
        }
        for (i = sp; i < abs(x1 - x2)+sp; i++) {
            if ((ClippingRect[0]<=i)&&(ClippingRect[1]<=(int)(dSlope * (double)i + dSection))&&(ClippingRect[2]>=i)&&(ClippingRect[3]>=(int)(dSlope * (double)i + dSection)))
                memcpy(pDispBuffer + XYtoOfset(i, (int)(dSlope * (double)i + dSection)), brg, sizeof(brg));
        }
    } else {
        if (y1 < y2) {
            sp = y1;
        } else {
            sp = y2;
        }
        for (i = sp; i < abs(y1 - y2)+sp; i++) {
            if ((ClippingRect[0]<=(int)((dSection*-1 + (double)i) / dSlope))&&(ClippingRect[1]<=i)&&(ClippingRect[2]>=(int)((dSection*-1 + (double)i) / dSlope))&&(ClippingRect[3]>=i))
                memcpy(pDispBuffer + XYtoOfset((int)((dSection*-1 + (double)i) / dSlope), i), brg, sizeof(brg));
        }
    }
}

//DrawRect : draw a filled rectangle
// int x1,y1 : start point coordinates
// int x2,y2 : end point coordinates
//
// Note: the color selected with the brush will be used.
void DrawRect(int x1, int y1, int x2, int y2) {
    if (pDispBuffer == NULL) return;
    for (int i1 = y1; i1 < y2; i1++) {
        for (int i2 = x1; i2 < x2; i2++) {
            if ((ClippingRect[0]<=i2)&&(ClippingRect[1]<=i1)&&(ClippingRect[2]>=i2)&&(ClippingRect[3]>=i1))
                memcpy(pDispBuffer + XYtoOfset(i2, i1), BrushColor, sizeof(BrushColor));
        }
    }
}

//DrawFrame : Draw a rectangle with only a frame
// int x1,y1 : start point coordinates
// int x2,y2 : end point coordinates
//
// Note: the color selected with the pen will be used.
void DrawFrame(int x1, int y1, int x2, int y2) {
    if (pDispBuffer == NULL) return;
    DrawLine(x1, y1, x2, y1);
    DrawLine(x2, y1, x2, y2 + 1);
    DrawLine(x2, y2, x1, y2);
    DrawLine(x1, y2, x1, y1);
}

//DrawCircle : draw a circle
// int x1,y1 : start point coordinates
// int x2,y2 : end point coordinates
// bool bPaint : whether to paint
//
// NOTE: The line will use the color selected with the pen.
// The color selected with the brush is used for filling.
void DrawCircle(int x1, int y1, int x2, int y2, bool bPaint) {
    if ((x1 == x2) || (y1 == y2)) return;
    int r = (x2 - x1) / 2, oldx = 0, oldy1 = 0, oldy2 = 0;
    double f = (double)(y2 - y1) / (double)(x2 - x1),tmp;
    int x = x1 + r, y = y1 + (int)((double)r*(double)f);
    if (bPaint) {
        for (int i = -r; i <= r; i++) {
            tmp = f*sqrt(r*r - i*i);
            DrawLineEx(x + i, y - tmp, x + i, y + tmp, (BrushColor[2] << 16) | (BrushColor[1] << 8) | BrushColor[0]);
        }
    }
    for (int i = -r; i <= r; i++) {
        tmp = f*sqrt(r*r - i*i);
        if (i == -r) {
            DrawDot(x + i, y - tmp);
            DrawDot(x + i, y + tmp);
        } else {
            DrawLine(x + i, y - tmp, oldx, oldy1);
            DrawLine(x + i, y + tmp, oldx, oldy2);
        }
        oldx = x + i;
        oldy1 = y - tmp;
        oldy2 = y + tmp;
    }
}

int main(void) {
    gfxInitDefault();
    gfxSetDoubleBuffering(GFX_TOP,true);

    int xpos = 190;
    int ypos = 110;

    while (aptMainLoop()) {
        hidScanInput();
        unsigned int keyDown = hidKeysDown();

        u8* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
        SelectFrame(fb);

        SetBrushColor(0,0,0);
        DrawRect(0,0,400,240);

        SetBrushColor(255,255,255);
        DrawCircle( xpos, ypos, xpos+20, ypos+20, true);
        if (keyDown & KEY_START) break;
        gfxFlushBuffers();
        gfxSwapBuffers();
        gspWaitForVBlank();
        };
    gfxExit();
    return 0;
};

Is there a library for drawing shapes with devkitpro on 3DS?
If anyone knows, please let me know.

↓The command prompt looks like this.
View attachment 332172
 
Last edited by Yukkuri_XP,

poisoned_soap

Member
Newcomer
Joined
May 10, 2020
Messages
7
Trophies
0
Age
20
XP
303
Country
United States
citro2d is the built-in library for drawing shapes, images and sprites.
You can use this site fo learn more about it, and I'm pretty sure you need to change the makefile in order for the program to actually compile.
 

Yukkuri_XP

New Member
OP
Newbie
Joined
Oct 14, 2022
Messages
4
Trophies
0
Location
ヰンドウズ
XP
65
Country
Japan
citro2d is the built-in library for drawing shapes, images and sprites.
You can use fo learn more about it, and I'm pretty sure you need to change the makefile in order for the program to actually compile.
Sorry, but is it possible to draw shapes only with gfxGetFramebuffer and memcpy?
I used to be able to use this module, but after updating devkitpro, it is no longer available.
C++:
void SelectFrame(u8* pFrame) {
    if (pFrame) {
        pDispBuffer = pFrame;
    }
}

void SetClipping(int x1, int y1, int x2, int y2) {
    ClippingRect[0] = x1;
    ClippingRect[1] = y1;
    ClippingRect[2] = x2;
    ClippingRect[3] = y2;
}
//SetPenColor
// unsigned char r,g,b
void SetPenColor(unsigned char r, unsigned char g, unsigned char b) {
    PenColor[2] = r;
    PenColor[1] = g;
    PenColor[0] = b;
}

void SetBrushColor(unsigned char r, unsigned char g, unsigned char b) {
    BrushColor[2] = r;
    BrushColor[1] = g;
    BrushColor[0] = b;
}

void DrawDot(int x1, int y1) {
    if (pDispBuffer == NULL) return;
    if ((ClippingRect[0]<=x1)&&(ClippingRect[1]<=y1)&&(ClippingRect[2]>=x1)&&(ClippingRect[3]>=y1))
        memcpy(pDispBuffer + XYtoOfset(x1, y1), PenColor, sizeof(PenColor));
}

void DrawLine(int x1, int y1, int x2, int y2) {
    DrawLineEx(x1, y1, x2, y2, (PenColor[2] << 16) | (PenColor[1] << 8) | PenColor[0]);
}
void DrawLineEx(int x1, int y1, int x2, int y2, int Color) {
    if (pDispBuffer == NULL) return;
    unsigned char brg[3] = { 0,0,0 };
    brg[2] = ((Color >> 16) & 0xFF);
    brg[1] = ((Color >> 8) & 0xFF);
    brg[0] = Color & 0xFF;
    int sp = 0, i = 0;
    double dSlope = 0.0, dSection = 0.0;
    if (x1 == x2) {
        if (y1 < y2) {
            sp = y1;
        } else {
            sp = y2;
        }
        for (i = sp; i < abs(y1 - y2)+sp; i++) {
            if ((ClippingRect[0]<=x1)&&(ClippingRect[1]<=i)&&(ClippingRect[2]>=x1)&&(ClippingRect[3]>=i))
                memcpy(pDispBuffer + XYtoOfset(x1, i), brg, sizeof(brg));
        }
        return;
    }
    dSlope = (double)(y1 - y2) / (double)(x1 - x2);
    dSection = (double)y1 - dSlope * x1;
    if (abs(x1 - x2) >= abs(y1 - y2)) {
        if (x1 < x2) {
            sp = x1;
        } else {
            sp = x2;
        }
        for (i = sp; i < abs(x1 - x2)+sp; i++) {
            if ((ClippingRect[0]<=i)&&(ClippingRect[1]<=(int)(dSlope * (double)i + dSection))&&(ClippingRect[2]>=i)&&(ClippingRect[3]>=(int)(dSlope * (double)i + dSection)))
                memcpy(pDispBuffer + XYtoOfset(i, (int)(dSlope * (double)i + dSection)), brg, sizeof(brg));
        }
    } else {
        if (y1 < y2) {
            sp = y1;
        } else {
            sp = y2;
        }
        for (i = sp; i < abs(y1 - y2)+sp; i++) {
            if ((ClippingRect[0]<=(int)((dSection*-1 + (double)i) / dSlope))&&(ClippingRect[1]<=i)&&(ClippingRect[2]>=(int)((dSection*-1 + (double)i) / dSlope))&&(ClippingRect[3]>=i))
                memcpy(pDispBuffer + XYtoOfset((int)((dSection*-1 + (double)i) / dSlope), i), brg, sizeof(brg));
        }
    }
}

void DrawRect(int x1, int y1, int x2, int y2) {
    if (pDispBuffer == NULL) return;
    for (int i1 = y1; i1 < y2; i1++) {
        for (int i2 = x1; i2 < x2; i2++) {
            if ((ClippingRect[0]<=i2)&&(ClippingRect[1]<=i1)&&(ClippingRect[2]>=i2)&&(ClippingRect[3]>=i1))
                memcpy(pDispBuffer + XYtoOfset(i2, i1), BrushColor, sizeof(BrushColor));
        }
    }
}

void DrawFrame(int x1, int y1, int x2, int y2) {
    if (pDispBuffer == NULL) return;
    DrawLine(x1, y1, x2, y1);
    DrawLine(x2, y1, x2, y2 + 1);
    DrawLine(x2, y2, x1, y2);
    DrawLine(x1, y2, x1, y1);
}

void DrawCircle(int x1, int y1, int x2, int y2, bool bPaint) {
    if ((x1 == x2) || (y1 == y2)) return;
    int r = (x2 - x1) / 2, oldx = 0, oldy1 = 0, oldy2 = 0;
    double f = (double)(y2 - y1) / (double)(x2 - x1),tmp;
    int x = x1 + r, y = y1 + (int)((double)r*(double)f);
    if (bPaint) {
        for (int i = -r; i <= r; i++) {
            tmp = f*sqrt(r*r - i*i);
            DrawLineEx(x + i, y - tmp, x + i, y + tmp, (BrushColor[2] << 16) | (BrushColor[1] << 8) | BrushColor[0]);
        }
    }
    for (int i = -r; i <= r; i++) {
        tmp = f*sqrt(r*r - i*i);
        if (i == -r) {
            DrawDot(x + i, y - tmp);
            DrawDot(x + i, y + tmp);
        } else {
            DrawLine(x + i, y - tmp, oldx, oldy1);
            DrawLine(x + i, y + tmp, oldx, oldy2);
        }
        oldx = x + i;
        oldy1 = y - tmp;
        oldy2 = y + tmp;
    }
}

void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, bool bPaint) {
}
*/
 
Last edited by Yukkuri_XP,

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    HiradeGirl @ HiradeGirl: Have a nice day. Life. Week. Month. year.