Guest User

BasicDrawing

a guest
May 26th, 2021
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <3ds.h>
  2. #include <citro3d.h>
  3. #include <citro2d.h>
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <math.h>
  9.  
  10. #define SCREENWIDTH 320
  11. #define SCREENHEIGHT 240
  12. #define PAGEWIDTH 512
  13. #define PAGEHEIGHT 512
  14. #define CPAD_DEADZONE 40
  15. #define CPAD_THEORETICALMAX 160
  16. #define CPAD_PAGECONST 1
  17. #define CPAD_PAGEMULT 0.02f
  18. #define CPAD_PAGECURVE 3.2f
  19.  
  20. //Return the new page position given the existing position and the circlepad input
  21. float calc_pagepos(circlePosition pos, float existing_pos)
  22. {
  23.    u16 cpadmag = abs(pos.dy);
  24.  
  25.    if(cpadmag > CPAD_DEADZONE)
  26.    {
  27.       return C2D_Clamp(existing_pos + (pos.dy < 0 ? 1 : -1) *
  28.             (CPAD_PAGECONST + pow(cpadmag * CPAD_PAGEMULT, CPAD_PAGECURVE)),
  29.             0, PAGEHEIGHT - SCREENHEIGHT);
  30.    }
  31.    else
  32.    {
  33.       return existing_pos;
  34.    }
  35. }
  36.  
  37. struct PageData {
  38.    Tex3DS_SubTexture subtex;     //Simple structures
  39.    C3D_Tex texture;
  40.    C2D_Image image;
  41.    C3D_RenderTarget * target;    //Actual data?
  42. };
  43.  
  44. void create_page(struct PageData * result, Tex3DS_SubTexture subtex)
  45. {
  46.    result->subtex = subtex;
  47.    C3D_TexInitVRAM(&(result->texture), subtex.width, subtex.height, GPU_RGBA5551);
  48.    result->target = C3D_RenderTargetCreateFromTex(&(result->texture), GPU_TEXFACE_2D, 0, -1);
  49.    result->image.tex = &(result->texture);
  50.    result->image.subtex = &(result->subtex);
  51. }
  52.  
  53. void clear_page(struct PageData page)
  54. {
  55.    C3D_RenderTargetDelete(page.target);
  56.    C3D_TexDelete(&page.texture);
  57. }
  58.  
  59. int main(int argc, char** argv)
  60. {
  61.    gfxInitDefault();
  62.    C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
  63.    C2D_Init(C2D_DEFAULT_MAX_OBJECTS);
  64.    C2D_Prepare();
  65.  
  66.    consoleInit(GFX_TOP, NULL);
  67.    C3D_RenderTarget* screen = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT);
  68.  
  69.    const u32 initial_color = C2D_Color32(0,0,0,0);
  70.    const u32 bg_color = C2D_Color32(255,255,255,255);
  71.  
  72.    const u32 color_a = C2D_Color32f(1,0,0,1.0f);
  73.    const u32 color_b = C2D_Color32f(1,1,0,1.0f);
  74.    const u32 color_x = C2D_Color32f(0,0,1,1.0f);
  75.    const u32 color_y = C2D_Color32f(0,1,0,1.0f);
  76.  
  77.    const u32* color_selected = &color_a;
  78.  
  79.    const Tex3DS_SubTexture subtex = {
  80.       PAGEWIDTH, PAGEHEIGHT,
  81.       0.0f, 1.0f, 1.0f, 0.0f
  82.    };
  83.  
  84.    struct PageData frontpg, backpg;
  85.    create_page(&frontpg, subtex);
  86.    create_page(&backpg, subtex);
  87.  
  88.    touchPosition current_touch;
  89.    touchPosition last_touch = current_touch; //Why? compiler warning shush
  90.    bool touching = false;
  91.    bool page_initialized = false;
  92.    u32 current_frame = 0;
  93.    float page_pos = 0;
  94.  
  95.    C3D_RenderTarget * target = frontpg.target;
  96.  
  97.    printf("Press ABXY to change color\n");
  98.    printf("Press SELECT to change layers\n");
  99.    printf("C-pad to scroll up/down\n");
  100.    printf("\nPress START to quit.\n");
  101.  
  102.  
  103.    while(aptMainLoop())
  104.    {
  105.       hidScanInput();
  106.       u32 kDown = hidKeysDown();
  107.       u32 kUp = hidKeysUp();
  108.       u32 kHeld = hidKeysHeld();
  109.  
  110.       // Respond to user input
  111.       if(kDown & KEY_START) break;
  112.  
  113.       if(kDown & KEY_A) color_selected = &color_a;
  114.       else if(kDown & KEY_B) color_selected = &color_b;
  115.       else if(kDown & KEY_X) color_selected = &color_x;
  116.       else if(kDown & KEY_Y) color_selected = &color_y;
  117.      
  118.       if(kDown & KEY_SELECT)
  119.       {
  120.          if(target == frontpg.target)
  121.          {
  122.             target = backpg.target;
  123.             printf("Changing to back layer\n");
  124.          }
  125.          else
  126.          {
  127.             target = frontpg.target;
  128.             printf("Changing to front layer\n");
  129.          }
  130.       }
  131.  
  132.       hidTouchRead(&current_touch);
  133.  
  134.       if(kDown & KEY_TOUCH) {
  135.          //Just throw away last touch from last time, we don't care
  136.          last_touch = current_touch;
  137.       }
  138.       if(kUp & KEY_TOUCH) {
  139.          //Nothing yet
  140.       }
  141.  
  142.       touching = (kHeld & KEY_TOUCH) > 0;
  143.  
  144.       circlePosition pos;
  145.         hidCircleRead(&pos);
  146.       page_pos = calc_pagepos(pos, page_pos);
  147.  
  148.       // Render the scene
  149.       C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
  150.  
  151.       if(!page_initialized)
  152.       {
  153.          C2D_TargetClear(frontpg.target, initial_color);
  154.          C2D_TargetClear(backpg.target, initial_color);
  155.          page_initialized = true;
  156.       }
  157.  
  158.       if(touching)
  159.       {
  160.          C2D_SceneBegin(target);
  161.  
  162.          //Calling JUST C2D_DrawLine (like I want) produces no output. Calling
  163.          //C2DDrawRectSolid beforehand makes it all work.
  164.          C2D_DrawRectSolid(current_touch.px - 2, current_touch.py - 2, 0.5f, 4, 4, *color_selected);
  165.          C2D_DrawLine(last_touch.px, last_touch.py + page_pos, *color_selected,
  166.                current_touch.px, current_touch.py + page_pos, *color_selected,
  167.                2, 0.5f);
  168.       }
  169.  
  170.       C2D_TargetClear(screen, bg_color);
  171.       C2D_SceneBegin(screen);
  172.       C2D_DrawImageAt(backpg.image, 0, -page_pos, 0.5f, NULL, 1.0f, 1.0f);
  173.       C2D_DrawImageAt(frontpg.image, 0, -page_pos, 0.5f, NULL, 1.0f, 1.0f);
  174.       C3D_FrameEnd(0);
  175.  
  176.       last_touch = current_touch;
  177.       current_frame++;
  178.    }
  179.  
  180.    clear_page(frontpg);
  181.    clear_page(backpg);
  182.  
  183.    C2D_Fini();
  184.    C3D_Fini();
  185.    gfxExit();
  186.    return 0;
  187. }
  188.  
RAW Paste Data