From 9e7e3b13117001b1deb510b736078c8043cdabf3 Mon Sep 17 00:00:00 2001 From: Cpasjuste Date: Tue, 2 Oct 2018 09:05:54 +0200 Subject: [PATCH] add sdl2 graphics example --- graphics/sdl2/Makefile | 193 ++++++++++++++++++++++++++++++++++ graphics/sdl2/source/main.cpp | 171 ++++++++++++++++++++++++++++++ 2 files changed, 364 insertions(+) create mode 100644 graphics/sdl2/Makefile create mode 100644 graphics/sdl2/source/main.cpp diff --git a/graphics/sdl2/Makefile b/graphics/sdl2/Makefile new file mode 100644 index 0000000..1e347eb --- /dev/null +++ b/graphics/sdl2/Makefile @@ -0,0 +1,193 @@ +#--------------------------------------------------------------------------------- +.SUFFIXES: +#--------------------------------------------------------------------------------- + +ifeq ($(strip $(DEVKITPRO)),) +$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=/devkitpro") +endif + +TOPDIR ?= $(CURDIR) +include $(DEVKITPRO)/libnx/switch_rules + +#--------------------------------------------------------------------------------- +# TARGET is the name of the output +# BUILD is the directory where object files & intermediate files will be placed +# SOURCES is a list of directories containing source code +# DATA is a list of directories containing data files +# INCLUDES is a list of directories containing header files +# EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm". +# ROMFS is the directory containing data to be added to RomFS, relative to the Makefile (Optional) +# +# NO_ICON: if set to anything, do not use icon. +# NO_NACP: if set to anything, no .nacp file is generated. +# APP_TITLE is the name of the app stored in the .nacp file (Optional) +# APP_AUTHOR is the author of the app stored in the .nacp file (Optional) +# APP_VERSION is the version of the app stored in the .nacp file (Optional) +# APP_TITLEID is the titleID of the app stored in the .nacp file (Optional) +# ICON is the filename of the icon (.jpg), relative to the project folder. +# If not set, it attempts to use one of the following (in this order): +# - .jpg +# - icon.jpg +# - /default_icon.jpg +#--------------------------------------------------------------------------------- +TARGET := $(notdir $(CURDIR)) +BUILD := build +SOURCES := source +DATA := data +INCLUDES := include +EXEFS_SRC := exefs_src +#ROMFS := romfs + +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- +ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE + +CFLAGS := -g -Wall -O2 -ffunction-sections \ + $(ARCH) $(DEFINES) + +CFLAGS += $(INCLUDE) -D__SWITCH__ + +CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions + +ASFLAGS := -g $(ARCH) +LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) + +LIBS := -lSDL2 -lglad -lEGL -lglapi -ldrm_nouveau -lnx + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := $(PORTLIBS) $(LIBNX) + + +#--------------------------------------------------------------------------------- +# no real need to edit anything past this point unless you need to add additional +# rules for different file extensions +#--------------------------------------------------------------------------------- +ifneq ($(BUILD),$(notdir $(CURDIR))) +#--------------------------------------------------------------------------------- + +export OUTPUT := $(CURDIR)/$(TARGET) +export TOPDIR := $(CURDIR) + +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) + +export DEPSDIR := $(CURDIR)/$(BUILD) + +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) +CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) +SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) + +#--------------------------------------------------------------------------------- +# use CXX for linking C++ projects, CC for standard C +#--------------------------------------------------------------------------------- +ifeq ($(strip $(CPPFILES)),) +#--------------------------------------------------------------------------------- + export LD := $(CC) +#--------------------------------------------------------------------------------- +else +#--------------------------------------------------------------------------------- + export LD := $(CXX) +#--------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------- + +export OFILES_BIN := $(addsuffix .o,$(BINFILES)) +export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) +export OFILES := $(OFILES_BIN) $(OFILES_SRC) +export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES))) + +export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ + $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ + -I$(CURDIR)/$(BUILD) + +export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) + +export BUILD_EXEFS_SRC := $(TOPDIR)/$(EXEFS_SRC) + +ifeq ($(strip $(ICON)),) + icons := $(wildcard *.jpg) + ifneq (,$(findstring $(TARGET).jpg,$(icons))) + export APP_ICON := $(TOPDIR)/$(TARGET).jpg + else + ifneq (,$(findstring icon.jpg,$(icons))) + export APP_ICON := $(TOPDIR)/icon.jpg + endif + endif +else + export APP_ICON := $(TOPDIR)/$(ICON) +endif + +ifeq ($(strip $(NO_ICON)),) + export NROFLAGS += --icon=$(APP_ICON) +endif + +ifeq ($(strip $(NO_NACP)),) + export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp +endif + +ifneq ($(APP_TITLEID),) + export NACPFLAGS += --titleid=$(APP_TITLEID) +endif + +ifneq ($(ROMFS),) + export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS) +endif + +.PHONY: $(BUILD) clean all + +#--------------------------------------------------------------------------------- +all: $(BUILD) + +$(BUILD): + @[ -d $@ ] || mkdir -p $@ + @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile + +#--------------------------------------------------------------------------------- +clean: + @echo clean ... + @rm -fr $(BUILD) $(TARGET).pfs0 $(TARGET).nso $(TARGET).nro $(TARGET).nacp $(TARGET).elf + + +#--------------------------------------------------------------------------------- +else +.PHONY: all + +DEPENDS := $(OFILES:.o=.d) + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +all : $(OUTPUT).pfs0 $(OUTPUT).nro + +$(OUTPUT).pfs0 : $(OUTPUT).nso + +$(OUTPUT).nso : $(OUTPUT).elf + +ifeq ($(strip $(NO_NACP)),) +$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp +else +$(OUTPUT).nro : $(OUTPUT).elf +endif + +$(OUTPUT).elf : $(OFILES) + +$(OFILES_SRC) : $(HFILES_BIN) + +#--------------------------------------------------------------------------------- +# you need a rule like this for each extension you use as binary data +#--------------------------------------------------------------------------------- +%.bin.o %_bin.h : %.bin +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------------- diff --git a/graphics/sdl2/source/main.cpp b/graphics/sdl2/source/main.cpp new file mode 100644 index 0000000..a603d16 --- /dev/null +++ b/graphics/sdl2/source/main.cpp @@ -0,0 +1,171 @@ +#include +#include + +#include +#include "SDL2/SDL.h" + +static SDL_DisplayMode modes[5]; + +static int mode_count = 0, current_mode = 0; + +void print_info(SDL_Window *window, SDL_Renderer *renderer) +{ + int w, h; + SDL_DisplayMode mode; + + SDL_GetWindowSize(window, &w, &h); + SDL_Log("window size: %i x %i\n", w, h); + SDL_GetRendererOutputSize(renderer, &w, &h); + SDL_Log("renderer size: %i x %i\n", w, h); + + SDL_GetCurrentDisplayMode(0, &mode); + SDL_Log("display mode: %i x %i @ %i bpp (%s)", + mode.w, mode.h, + SDL_BITSPERPIXEL(mode.format), + SDL_GetPixelFormatName(mode.format)); +} + +void change_mode(SDL_Window *window) +{ + current_mode++; + if (current_mode == mode_count) { + current_mode = 0; + } + + SDL_SetWindowDisplayMode(window, &modes[current_mode]); +} + +void draw_rects(SDL_Renderer *renderer, int x, int y) +{ + // R + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + SDL_Rect r = {x, y, 64, 64}; + SDL_RenderFillRect(renderer, &r); + + // G + SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); + SDL_Rect g = {x + 64, y, 64, 64}; + SDL_RenderFillRect(renderer, &g); + + // B + SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); + SDL_Rect b = {x + 128, y, 64, 64}; + SDL_RenderFillRect(renderer, &b); +} + +int main(int argc, char *argv[]) +{ + SDL_Event event; + SDL_Window *window; + SDL_Renderer *renderer; + int done = 0, x = 0, w, h; + + // mandatory at least on switch, else gfx is not properly closed + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { + SDL_Log("SDL_Init: %s\n", SDL_GetError()); + return -1; + } + + // create a window (OpenGL always enabled) + // available switch SDL2 video modes : + // 1920 x 1080 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) (docked only) + // 1280 x 720 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) + // 960 x 540 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) + // 800 x 600 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) + // 640 x 480 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) + window = SDL_CreateWindow("sdl2_gles2", 0, 0, 640, 480, SDL_WINDOW_FULLSCREEN); + if (!window) { + SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError()); + SDL_Quit(); + return -1; + } + + // create a renderer (OpenGL ES2) + renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if (!renderer) { + SDL_Log("SDL_CreateRenderer: %s\n", SDL_GetError()); + SDL_Quit(); + return -1; + } + + // print some info about display/window/renderer, for "educational purpose" + print_info(window, renderer); + + // list available display modes, for "educational purpose" + mode_count = SDL_GetNumDisplayModes(0); + for (int i = 0; i < mode_count; i++) { + SDL_DisplayMode mode; + SDL_GetDisplayMode(0, i, &mode); + modes[i] = mode; + } + + // open CONTROLLER_PLAYER_1 and CONTROLLER_PLAYER_2 + // when railed, both joycons are mapped to joystick #0, + // else joycons are individually mapped to joystick #0, joystick #1, ... + // https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L45 + for (int i = 0; i < 2; i++) { + if (SDL_JoystickOpen(i) == NULL) { + SDL_Log("SDL_JoystickOpen: %s\n", SDL_GetError()); + SDL_Quit(); + return -1; + } + } + + while (!done) { + + while (SDL_PollEvent(&event)) { + + switch (event.type) { + + case SDL_JOYAXISMOTION: + SDL_Log("Joystick %d axis %d value: %d\n", + event.jaxis.which, + event.jaxis.axis, event.jaxis.value); + break; + + case SDL_JOYBUTTONDOWN: + SDL_Log("Joystick %d button %d down\n", + event.jbutton.which, event.jbutton.button); + // seek for joystick #0 down (A) + // https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L52 + if (event.jbutton.which == 0 && event.jbutton.button == 0) { + change_mode(window); + print_info(window, renderer); + } + // seek for joystick #0 down (B) + if (event.jbutton.which == 0 && event.jbutton.button == 1) { + done = 1; + } + break; + + default: + break; + } + } + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + // Fill renderer bounds + SDL_SetRenderDrawColor(renderer, 111, 111, 111, 255); + SDL_GetRendererOutputSize(renderer, &w, &h); + SDL_Rect f = {0, 0, w, h}; + SDL_RenderFillRect(renderer, &f); + + draw_rects(renderer, x, 0); + draw_rects(renderer, x, h - 64); + + SDL_RenderPresent(renderer); + + x++; + if (x > w - 192) { + x = 0; + } + } + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; +}