Skip to content

Commit e49924e

Browse files
Swiftlokefincs
authored andcommitted
Add 2d_shapes example for citro2d (#25)
1 parent 3bec421 commit e49924e

File tree

2 files changed

+313
-0
lines changed

2 files changed

+313
-0
lines changed

graphics/gpu/2d_shapes/Makefile

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
#---------------------------------------------------------------------------------
2+
.SUFFIXES:
3+
#---------------------------------------------------------------------------------
4+
5+
ifeq ($(strip $(DEVKITARM)),)
6+
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
7+
endif
8+
9+
TOPDIR ?= $(CURDIR)
10+
include $(DEVKITARM)/3ds_rules
11+
12+
#---------------------------------------------------------------------------------
13+
# TARGET is the name of the output
14+
# BUILD is the directory where object files & intermediate files will be placed
15+
# SOURCES is a list of directories containing source code
16+
# DATA is a list of directories containing data files
17+
# INCLUDES is a list of directories containing header files
18+
# GRAPHICS is a list of directories containing graphics files
19+
# GFXBUILD is the directory where converted graphics files will be placed
20+
# If set to $(BUILD), it will statically link in the converted
21+
# files as if they were data files.
22+
#
23+
# NO_SMDH: if set to anything, no SMDH file is generated.
24+
# ROMFS is the directory which contains the RomFS, relative to the Makefile (Optional)
25+
# APP_TITLE is the name of the app stored in the SMDH file (Optional)
26+
# APP_DESCRIPTION is the description of the app stored in the SMDH file (Optional)
27+
# APP_AUTHOR is the author of the app stored in the SMDH file (Optional)
28+
# ICON is the filename of the icon (.png), relative to the project folder.
29+
# If not set, it attempts to use one of the following (in this order):
30+
# - <Project name>.png
31+
# - icon.png
32+
# - <libctru folder>/default_icon.png
33+
#---------------------------------------------------------------------------------
34+
TARGET := $(notdir $(CURDIR))
35+
BUILD := build
36+
SOURCES := source
37+
DATA := data
38+
INCLUDES := include
39+
GRAPHICS := gfx
40+
#GFXBUILD := $(BUILD)
41+
ROMFS := romfs
42+
GFXBUILD := $(ROMFS)/gfx
43+
44+
#---------------------------------------------------------------------------------
45+
# options for code generation
46+
#---------------------------------------------------------------------------------
47+
ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft
48+
49+
CFLAGS := -g -Wall -O2 -mword-relocations \
50+
-fomit-frame-pointer -ffunction-sections \
51+
$(ARCH)
52+
53+
CFLAGS += $(INCLUDE) -DARM11 -D_3DS
54+
55+
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11
56+
57+
ASFLAGS := -g $(ARCH)
58+
LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
59+
60+
LIBS := -lcitro2d -lcitro3d -lctru -lm
61+
62+
#---------------------------------------------------------------------------------
63+
# list of directories containing libraries, this must be the top level containing
64+
# include and lib
65+
#---------------------------------------------------------------------------------
66+
LIBDIRS := $(CTRULIB)
67+
68+
69+
#---------------------------------------------------------------------------------
70+
# no real need to edit anything past this point unless you need to add additional
71+
# rules for different file extensions
72+
#---------------------------------------------------------------------------------
73+
ifneq ($(BUILD),$(notdir $(CURDIR)))
74+
#---------------------------------------------------------------------------------
75+
76+
export OUTPUT := $(CURDIR)/$(TARGET)
77+
export TOPDIR := $(CURDIR)
78+
79+
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
80+
$(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir)) \
81+
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
82+
83+
export DEPSDIR := $(CURDIR)/$(BUILD)
84+
85+
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
86+
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
87+
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
88+
PICAFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.v.pica)))
89+
SHLISTFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.shlist)))
90+
GFXFILES := $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.t3s)))
91+
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
92+
93+
#---------------------------------------------------------------------------------
94+
# use CXX for linking C++ projects, CC for standard C
95+
#---------------------------------------------------------------------------------
96+
ifeq ($(strip $(CPPFILES)),)
97+
#---------------------------------------------------------------------------------
98+
export LD := $(CC)
99+
#---------------------------------------------------------------------------------
100+
else
101+
#---------------------------------------------------------------------------------
102+
export LD := $(CXX)
103+
#---------------------------------------------------------------------------------
104+
endif
105+
#---------------------------------------------------------------------------------
106+
107+
export T3XFILES := $(GFXFILES:.t3s=.t3x)
108+
109+
export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
110+
111+
export OFILES_BIN := $(addsuffix .o,$(BINFILES)) \
112+
$(PICAFILES:.v.pica=.shbin.o) $(SHLISTFILES:.shlist=.shbin.o) \
113+
$(if $(filter $(BUILD),$(GFXBUILD)),$(addsuffix .o,$(T3XFILES)))
114+
115+
export OFILES := $(OFILES_BIN) $(OFILES_SOURCES)
116+
117+
export HFILES := $(PICAFILES:.v.pica=_shbin.h) $(SHLISTFILES:.shlist=_shbin.h) \
118+
$(addsuffix .h,$(subst .,_,$(BINFILES))) \
119+
$(GFXFILES:.t3s=.h)
120+
121+
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
122+
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
123+
-I$(CURDIR)/$(BUILD)
124+
125+
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
126+
127+
export _3DSXDEPS := $(if $(NO_SMDH),,$(OUTPUT).smdh)
128+
129+
ifeq ($(strip $(ICON)),)
130+
icons := $(wildcard *.png)
131+
ifneq (,$(findstring $(TARGET).png,$(icons)))
132+
export APP_ICON := $(TOPDIR)/$(TARGET).png
133+
else
134+
ifneq (,$(findstring icon.png,$(icons)))
135+
export APP_ICON := $(TOPDIR)/icon.png
136+
endif
137+
endif
138+
else
139+
export APP_ICON := $(TOPDIR)/$(ICON)
140+
endif
141+
142+
ifeq ($(strip $(NO_SMDH)),)
143+
export _3DSXFLAGS += --smdh=$(CURDIR)/$(TARGET).smdh
144+
endif
145+
146+
ifneq ($(ROMFS),)
147+
export _3DSXFLAGS += --romfs=$(CURDIR)/$(ROMFS)
148+
endif
149+
150+
.PHONY: all clean
151+
152+
#---------------------------------------------------------------------------------
153+
all:
154+
@mkdir -p $(BUILD) $(GFXBUILD)
155+
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
156+
157+
#---------------------------------------------------------------------------------
158+
clean:
159+
@echo clean ...
160+
@rm -fr $(BUILD) $(TARGET).3dsx $(OUTPUT).smdh $(TARGET).elf
161+
162+
163+
#---------------------------------------------------------------------------------
164+
else
165+
166+
#---------------------------------------------------------------------------------
167+
# main targets
168+
#---------------------------------------------------------------------------------
169+
$(OUTPUT).3dsx : $(OUTPUT).elf $(_3DSXDEPS)
170+
171+
$(OFILES_SOURCES) : $(HFILES)
172+
173+
$(OUTPUT).elf : $(OFILES)
174+
175+
#---------------------------------------------------------------------------------
176+
# you need a rule like this for each extension you use as binary data
177+
#---------------------------------------------------------------------------------
178+
%.bin.o %_bin.h : %.bin
179+
#---------------------------------------------------------------------------------
180+
@echo $(notdir $<)
181+
@$(bin2o)
182+
183+
#---------------------------------------------------------------------------------
184+
.PRECIOUS : %.t3x
185+
%.t3x.o %_t3x.h : %.t3x
186+
#---------------------------------------------------------------------------------
187+
@$(bin2o)
188+
189+
#---------------------------------------------------------------------------------
190+
# rules for assembling GPU shaders
191+
#---------------------------------------------------------------------------------
192+
define shader-as
193+
$(eval CURBIN := $*.shbin)
194+
$(eval DEPSFILE := $(DEPSDIR)/$*.shbin.d)
195+
echo "$(CURBIN).o: $< $1" > $(DEPSFILE)
196+
echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"_end[];" > `(echo $(CURBIN) | tr . _)`.h
197+
echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> `(echo $(CURBIN) | tr . _)`.h
198+
echo "extern const u32" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> `(echo $(CURBIN) | tr . _)`.h
199+
picasso -o $(CURBIN) $1
200+
bin2s $(CURBIN) | $(AS) -o $*.shbin.o
201+
endef
202+
203+
%.shbin.o %_shbin.h : %.v.pica %.g.pica
204+
@echo $(notdir $^)
205+
@$(call shader-as,$^)
206+
207+
%.shbin.o %_shbin.h : %.v.pica
208+
@echo $(notdir $<)
209+
@$(call shader-as,$<)
210+
211+
%.shbin.o %_shbin.h : %.shlist
212+
@echo $(notdir $<)
213+
@$(call shader-as,$(foreach file,$(shell cat $<),$(dir $<)$(file)))
214+
215+
#---------------------------------------------------------------------------------
216+
%.t3x %.h : %.t3s
217+
#---------------------------------------------------------------------------------
218+
@echo $(notdir $<)
219+
@tex3ds -i $< -H $*.h -d $*.d -o $(TOPDIR)/$(GFXBUILD)/$*.t3x
220+
221+
-include $(DEPSDIR)/*.d
222+
223+
#---------------------------------------------------------------------------------------
224+
endif
225+
#---------------------------------------------------------------------------------------
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Simple citro2d untextured shape example
2+
#include <citro2d.h>
3+
4+
#include <string.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
9+
#define SCREEN_WIDTH 400
10+
#define SCREEN_HEIGHT 240
11+
12+
//---------------------------------------------------------------------------------
13+
int main(int argc, char* argv[]) {
14+
//---------------------------------------------------------------------------------
15+
// Init libs
16+
gfxInitDefault();
17+
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
18+
C2D_Init(C2D_DEFAULT_MAX_OBJECTS);
19+
C2D_Prepare();
20+
consoleInit(GFX_BOTTOM, NULL);
21+
22+
// Create screens
23+
C3D_RenderTarget* top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);
24+
25+
// Create colors
26+
u32 clrWhite = C2D_Color32(0xFF, 0xFF, 0xFF, 0xFF);
27+
u32 clrGreen = C2D_Color32(0x00, 0xFF, 0x00, 0xFF);
28+
u32 clrRed = C2D_Color32(0xFF, 0x00, 0x00, 0xFF);
29+
u32 clrBlue = C2D_Color32(0x00, 0x00, 0xFF, 0xFF);
30+
31+
u32 clrCircle1 = C2D_Color32(0xFF, 0x00, 0xFF, 0xFF);
32+
u32 clrCircle2 = C2D_Color32(0xFF, 0xFF, 0x00, 0xFF);
33+
u32 clrCircle3 = C2D_Color32(0x00, 0xFF, 0xFF, 0xFF);
34+
35+
u32 clrSolidCircle = C2D_Color32(0x68, 0xB0, 0xD8, 0xFF);
36+
37+
u32 clrTri1 = C2D_Color32(0xFF, 0x15, 0x00, 0xFF);
38+
u32 clrTri2 = C2D_Color32(0x27, 0x69, 0xE5, 0xFF);
39+
40+
u32 clrRec1 = C2D_Color32(0x9A, 0x6C, 0xB9, 0xFF);
41+
u32 clrRec2 = C2D_Color32(0xFF, 0xFF, 0x2C, 0xFF);
42+
u32 clrRec3 = C2D_Color32(0xD8, 0xF6, 0x0F, 0xFF);
43+
u32 clrRec4 = C2D_Color32(0x40, 0xEA, 0x87, 0xFF);
44+
45+
u32 clrClear = C2D_Color32(0xFF, 0xD8, 0xB0, 0x68);
46+
47+
// Main loop
48+
while (aptMainLoop())
49+
{
50+
hidScanInput();
51+
52+
// Respond to user input
53+
u32 kDown = hidKeysDown();
54+
if (kDown & KEY_START)
55+
break; // break in order to return to hbmenu
56+
printf("\x1b[1;1HSimple citro2d shapes example");
57+
printf("\x1b[2;1HCPU: %6.2f%%\x1b[K", C3D_GetProcessingTime()*6.0f);
58+
printf("\x1b[3;1HGPU: %6.2f%%\x1b[K", C3D_GetDrawingTime()*6.0f);
59+
printf("\x1b[4;1HCmdBuf: %6.2f%%\x1b[K", C3D_GetCmdBufUsage()*100.0f);
60+
61+
// Render the scene
62+
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
63+
C2D_TargetClear(top, clrClear);
64+
C2D_SceneBegin(top);
65+
66+
C2D_DrawTriangle(50 / 2, SCREEN_HEIGHT - 50, clrWhite,
67+
0, SCREEN_HEIGHT, clrTri1,
68+
50, SCREEN_HEIGHT, clrTri2, 0);
69+
C2D_DrawRectangle(SCREEN_WIDTH - 50, 0, 0, 50, 50, clrRec1, clrRec2, clrRec3, clrRec4);
70+
71+
// Circles require a state change (an expensive operation) within citro2d's internals, so draw them last.
72+
// Although it is possible to draw them in the middle of drawing non-circular objects
73+
// (sprites, images, triangles, rectangles, etc.) this is not recommended. They should either
74+
// be drawn before all non-circular objects, or afterwards.
75+
C2D_DrawEllipse(0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, clrCircle1, clrCircle2, clrCircle3, clrWhite);
76+
C2D_DrawCircle(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 0, 50, clrCircle3, clrWhite, clrCircle1, clrCircle2);
77+
C2D_DrawCircle(25, 25, 0, 25,
78+
clrRed, clrBlue, clrGreen, clrWhite);
79+
C2D_DrawCircleSolid(SCREEN_WIDTH - 25, SCREEN_HEIGHT - 25, 0, 25, clrSolidCircle);
80+
C3D_FrameEnd(0);
81+
}
82+
83+
// Deinit libs
84+
C2D_Fini();
85+
C3D_Fini();
86+
gfxExit();
87+
return 0;
88+
}

0 commit comments

Comments
 (0)