mirror of
https://github.com/switchbrew/switch-examples.git
synced 2025-06-21 13:22:40 +02:00
Added ttf example to sdl2-demo
This commit is contained in:
parent
73b50688ab
commit
cb6412c2b1
@ -62,7 +62,7 @@ CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
|
|||||||
ASFLAGS := -g $(ARCH)
|
ASFLAGS := -g $(ARCH)
|
||||||
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
|
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
|
||||||
|
|
||||||
LIBS := `$(PREFIX)pkg-config --libs sdl2 SDL2_mixer SDL2_image` \
|
LIBS := `$(PREFIX)pkg-config --libs sdl2 SDL2_mixer SDL2_image SDL2_ttf` \
|
||||||
-lnx
|
-lnx
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------------
|
||||||
|
BIN
graphics/sdl2/sdl2-demo/romfs/data/LeroyLetteringLightBeta01.ttf
Normal file
BIN
graphics/sdl2/sdl2-demo/romfs/data/LeroyLetteringLightBeta01.ttf
Normal file
Binary file not shown.
@ -1,5 +1,5 @@
|
|||||||
/* Mini SDL Demo
|
/* Mini SDL Demo
|
||||||
* featuring SDL2 + SDL2_mixer + SDL2_image
|
* featuring SDL2 + SDL2_mixer + SDL2_image + SDL2_ttf
|
||||||
* on Nintendo Switch using libnx
|
* on Nintendo Switch using libnx
|
||||||
*
|
*
|
||||||
* Copyright 2018 carsten1ns
|
* Copyright 2018 carsten1ns
|
||||||
@ -25,6 +25,7 @@
|
|||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <SDL_mixer.h>
|
#include <SDL_mixer.h>
|
||||||
#include <SDL_image.h>
|
#include <SDL_image.h>
|
||||||
|
#include <SDL_ttf.h>
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
|
|
||||||
// some switch buttons
|
// some switch buttons
|
||||||
@ -42,6 +43,23 @@
|
|||||||
#define SCREEN_W 1280
|
#define SCREEN_W 1280
|
||||||
#define SCREEN_H 720
|
#define SCREEN_H 720
|
||||||
|
|
||||||
|
void render_text(SDL_Renderer *renderer, int x, int y, const char* text, TTF_Font *font, SDL_Rect *rect, SDL_Color *color)
|
||||||
|
{
|
||||||
|
SDL_Surface *surface;
|
||||||
|
SDL_Texture *texture;
|
||||||
|
|
||||||
|
surface = TTF_RenderText_Solid(font, text, *color);
|
||||||
|
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||||
|
rect->x = x;
|
||||||
|
rect->y = y;
|
||||||
|
rect->w = surface->w;
|
||||||
|
rect->h = surface->h;
|
||||||
|
|
||||||
|
SDL_FreeSurface(surface);
|
||||||
|
SDL_RenderCopy(renderer, texture, NULL, rect);
|
||||||
|
SDL_DestroyTexture(texture);
|
||||||
|
}
|
||||||
|
|
||||||
int rand_range(int min, int max){
|
int rand_range(int min, int max){
|
||||||
return min + rand() / (RAND_MAX / (max - min + 1) + 1);
|
return min + rand() / (RAND_MAX / (max - min + 1) + 1);
|
||||||
}
|
}
|
||||||
@ -55,12 +73,13 @@ int main(int argc, char** argv) {
|
|||||||
int exit_requested = 0;
|
int exit_requested = 0;
|
||||||
int trail = 0;
|
int trail = 0;
|
||||||
int wait = 25;
|
int wait = 25;
|
||||||
|
|
||||||
SDL_Texture *switchlogo_tex = NULL, *sdllogo_tex = NULL;
|
SDL_Texture *switchlogo_tex = NULL, *sdllogo_tex = NULL;
|
||||||
SDL_Rect pos = { 0, 0, 0, 0 }, sdl_pos = { 0, 0, 0, 0 };
|
SDL_Rect pos = { 0, 0, 0, 0 }, sdl_pos = { 0, 0, 0, 0 };
|
||||||
Mix_Music *music = NULL;
|
Mix_Music *music = NULL;
|
||||||
Mix_Chunk *sound[4] = { NULL };
|
Mix_Chunk *sound[4] = { NULL };
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
|
SDL_Rect rect;
|
||||||
|
|
||||||
SDL_Color colors[] = {
|
SDL_Color colors[] = {
|
||||||
{ 128, 128, 128, 0 }, // gray
|
{ 128, 128, 128, 0 }, // gray
|
||||||
@ -81,8 +100,9 @@ int main(int argc, char** argv) {
|
|||||||
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
|
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
|
||||||
Mix_Init(MIX_INIT_OGG);
|
Mix_Init(MIX_INIT_OGG);
|
||||||
IMG_Init(IMG_INIT_PNG);
|
IMG_Init(IMG_INIT_PNG);
|
||||||
|
TTF_Init();
|
||||||
|
|
||||||
SDL_Window* window = SDL_CreateWindow("sdl2+mixer+image demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_W, SCREEN_H, SDL_WINDOW_SHOWN);
|
SDL_Window* window = SDL_CreateWindow("sdl2+mixer+image+ttf demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_W, SCREEN_H, SDL_WINDOW_SHOWN);
|
||||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
|
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
|
||||||
|
|
||||||
// load logos from file
|
// load logos from file
|
||||||
@ -109,6 +129,9 @@ int main(int argc, char** argv) {
|
|||||||
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
||||||
SDL_JoystickEventState(SDL_ENABLE);
|
SDL_JoystickEventState(SDL_ENABLE);
|
||||||
SDL_JoystickOpen(0);
|
SDL_JoystickOpen(0);
|
||||||
|
|
||||||
|
// load font from romfs
|
||||||
|
TTF_Font* font = TTF_OpenFont("data/LeroyLetteringLightBeta01.ttf", 36);
|
||||||
|
|
||||||
SDL_InitSubSystem(SDL_INIT_AUDIO);
|
SDL_InitSubSystem(SDL_INIT_AUDIO);
|
||||||
Mix_AllocateChannels(5);
|
Mix_AllocateChannels(5);
|
||||||
@ -196,6 +219,9 @@ int main(int argc, char** argv) {
|
|||||||
SDL_SetTextureColorMod(switchlogo_tex, colors[col].r, colors[col].g, colors[col].b);
|
SDL_SetTextureColorMod(switchlogo_tex, colors[col].r, colors[col].g, colors[col].b);
|
||||||
SDL_RenderCopy(renderer, switchlogo_tex, NULL, &pos);
|
SDL_RenderCopy(renderer, switchlogo_tex, NULL, &pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// put text on screen
|
||||||
|
render_text(renderer, 0, SCREEN_H - 36, "Hello, world!", font, &rect, &colors[1]);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
@ -211,6 +237,7 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
IMG_Quit();
|
IMG_Quit();
|
||||||
Mix_CloseAudio();
|
Mix_CloseAudio();
|
||||||
|
TTF_Quit();
|
||||||
Mix_Quit();
|
Mix_Quit();
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
romfsExit();
|
romfsExit();
|
||||||
|
Loading…
Reference in New Issue
Block a user