mirror of
https://github.com/switchbrew/switch-examples.git
synced 2025-06-20 21:12:38 +02:00
Added sdl2-ttf example to sdl2-demo (#80)
This commit is contained in:
parent
73b50688ab
commit
ea27fe123d
@ -62,7 +62,7 @@ CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
|
||||
ASFLAGS := -g $(ARCH)
|
||||
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
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
|
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
|
||||
* featuring SDL2 + SDL2_mixer + SDL2_image
|
||||
* featuring SDL2 + SDL2_mixer + SDL2_image + SDL2_ttf
|
||||
* on Nintendo Switch using libnx
|
||||
*
|
||||
* Copyright 2018 carsten1ns
|
||||
@ -25,6 +25,7 @@
|
||||
#include <SDL.h>
|
||||
#include <SDL_mixer.h>
|
||||
#include <SDL_image.h>
|
||||
#include <SDL_ttf.h>
|
||||
#include <switch.h>
|
||||
|
||||
// some switch buttons
|
||||
@ -42,6 +43,23 @@
|
||||
#define SCREEN_W 1280
|
||||
#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){
|
||||
return min + rand() / (RAND_MAX / (max - min + 1) + 1);
|
||||
}
|
||||
@ -61,6 +79,7 @@ int main(int argc, char** argv) {
|
||||
Mix_Music *music = NULL;
|
||||
Mix_Chunk *sound[4] = { NULL };
|
||||
SDL_Event event;
|
||||
SDL_Rect rect;
|
||||
|
||||
SDL_Color colors[] = {
|
||||
{ 128, 128, 128, 0 }, // gray
|
||||
@ -81,8 +100,9 @@ int main(int argc, char** argv) {
|
||||
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
|
||||
Mix_Init(MIX_INIT_OGG);
|
||||
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);
|
||||
|
||||
// load logos from file
|
||||
@ -109,6 +129,9 @@ int main(int argc, char** argv) {
|
||||
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
||||
SDL_JoystickEventState(SDL_ENABLE);
|
||||
SDL_JoystickOpen(0);
|
||||
|
||||
// load font from romfs
|
||||
TTF_Font* font = TTF_OpenFont("data/LeroyLetteringLightBeta01.ttf", 36);
|
||||
|
||||
SDL_InitSubSystem(SDL_INIT_AUDIO);
|
||||
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_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);
|
||||
|
||||
@ -211,6 +237,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
IMG_Quit();
|
||||
Mix_CloseAudio();
|
||||
TTF_Quit();
|
||||
Mix_Quit();
|
||||
SDL_Quit();
|
||||
romfsExit();
|
||||
|
Loading…
Reference in New Issue
Block a user