SDL_ttf 清除我之前和未来的所有渲染
SDL_ttf clearing all my prior and future renders
我一直在努力让 SDL 和 SDL_ttf 一起工作。我的最小可重现案例(下方)在 VC++ 中工作,但对于我来说,我无法让它在 MinGW 环境中工作。
- SDL 版本:2.0.10(撰写本文时最新版本)
- SDL_ttf 版本:2.0.15(撰写本文时最新版本)
我的设置如下所示:
- 已复制 SDL 和 SDL_ttf 库并将文件夹包含到我的项目根目录中。
- 设置我的 link 并像这样包含在我的 Makefile 中:
SRC_DIR=./app/src
INC_DIR=./app/inc
OBJ_DIR=./.obj
BUILD_DIR=./build
NAME=$(BUILD_DIR)/out
all: $(NAME)
CC=gcc
CXX=g++
CXXFLAGS=-Wall -Wextra -pedantic --std=c++17 -g
# Objects to build
STNAMES=\
main \
OBJECTS=$(patsubst %,$(OBJ_DIR)/%.o, $(STNAMES))
# Windows specific settings
ifeq ($(OS), Windows_NT)
LINK=\
-L./SDL2/lib \
-L./SDL2_ttf/lib \
-lmingw32 \
-lSDL2main \
-lSDL2 \
-lSDL2_ttf \
-lstdc++ \
INCLUDE=\
-I$(INC_DIR) \
-I./SDL2/include \
-I./SDL2_ttf/include \
define mkdir =
if not exist "$(1)" mkdir "$(1)"
endef
define rmdir =
if exist "$(1)" del /S/Q "$(1)"
endef
EXECUTABLE=$(NAME).exe
# macOS specific settings
# UNTESTED, assumes dependencies are installed with brew
else
LINK=$(shell sdl2-config --libs) -lc++
INCLUDE=\
-I$(shell brew --prefix)/include/SDL2 \
define mkdir
mkdir -p $(1)
endef
define rmdir =
rm -rf $(1)
endef
EXECUTABLE=$(NAME)
endif
$(NAME): $(OBJECTS)
@$(call mkdir,$(BUILD_DIR))
$(CC) -o $@ $(OBJECTS) $(LINK)
@$(EXECUTABLE)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
@$(call mkdir,$(OBJ_DIR))
@$(CXX) -o $@ -c $< $(CXXFLAGS) $(INCLUDE)
re:
@$(call rmdir,$(OBJ_DIR))
make
- 将 dll 添加到 MinGW 的
bin
文件夹中。
这是一个最小的可重现测试用例,它适用于 Visual Studio,但不适用于 MinGW:
#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>
void drawText(SDL_Renderer* renderer, TTF_Font* font, int x, int y, const char* text) {
auto surface = TTF_RenderText_Solid(font, text, SDL_Color{ 255, 255, 255, 255 });
auto texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect dstRect = { x, y, surface->w, surface->h };
SDL_RenderCopy(renderer, texture, NULL, &dstRect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("some title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1920, 1080, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
TTF_Init();
bool running = true;
SDL_Event event;
auto font = TTF_OpenFont("assets/courier_prime.ttf", 36);
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
running = false;
}
SDL_SetRenderDrawColor(renderer, 25, 25, 25, 255);
SDL_RenderClear(renderer);
drawText(renderer, font, 10, 10, "Test string");
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255 );
auto rect = SDL_Rect{10, 100, 110, 200};
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);
}
TTF_CloseFont(font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
}
无论我将 SDL_RenderFillRect
放在哪里(在 drawText 之前或之后),我唯一看到的就是文本。矩形不再可见。
我目前正在处理一个混合的 OS 开发环境(mac 和 windows),因此我希望使用 make 在两者的基础上进行构建。
似乎是 SDL_RENDERER_ACCELERATED 和 openGL 未链接的问题。链接 openGL 没有解决问题,但是,切换到软件渲染器 (SDL_RENDERER_SOFTWARE) 确实修复了渲染错误。很奇怪,它确实适用于 VS。我想这仍然是个谜...
我一直在努力让 SDL 和 SDL_ttf 一起工作。我的最小可重现案例(下方)在 VC++ 中工作,但对于我来说,我无法让它在 MinGW 环境中工作。
- SDL 版本:2.0.10(撰写本文时最新版本)
- SDL_ttf 版本:2.0.15(撰写本文时最新版本)
我的设置如下所示:
- 已复制 SDL 和 SDL_ttf 库并将文件夹包含到我的项目根目录中。
- 设置我的 link 并像这样包含在我的 Makefile 中:
SRC_DIR=./app/src
INC_DIR=./app/inc
OBJ_DIR=./.obj
BUILD_DIR=./build
NAME=$(BUILD_DIR)/out
all: $(NAME)
CC=gcc
CXX=g++
CXXFLAGS=-Wall -Wextra -pedantic --std=c++17 -g
# Objects to build
STNAMES=\
main \
OBJECTS=$(patsubst %,$(OBJ_DIR)/%.o, $(STNAMES))
# Windows specific settings
ifeq ($(OS), Windows_NT)
LINK=\
-L./SDL2/lib \
-L./SDL2_ttf/lib \
-lmingw32 \
-lSDL2main \
-lSDL2 \
-lSDL2_ttf \
-lstdc++ \
INCLUDE=\
-I$(INC_DIR) \
-I./SDL2/include \
-I./SDL2_ttf/include \
define mkdir =
if not exist "$(1)" mkdir "$(1)"
endef
define rmdir =
if exist "$(1)" del /S/Q "$(1)"
endef
EXECUTABLE=$(NAME).exe
# macOS specific settings
# UNTESTED, assumes dependencies are installed with brew
else
LINK=$(shell sdl2-config --libs) -lc++
INCLUDE=\
-I$(shell brew --prefix)/include/SDL2 \
define mkdir
mkdir -p $(1)
endef
define rmdir =
rm -rf $(1)
endef
EXECUTABLE=$(NAME)
endif
$(NAME): $(OBJECTS)
@$(call mkdir,$(BUILD_DIR))
$(CC) -o $@ $(OBJECTS) $(LINK)
@$(EXECUTABLE)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
@$(call mkdir,$(OBJ_DIR))
@$(CXX) -o $@ -c $< $(CXXFLAGS) $(INCLUDE)
re:
@$(call rmdir,$(OBJ_DIR))
make
- 将 dll 添加到 MinGW 的
bin
文件夹中。
这是一个最小的可重现测试用例,它适用于 Visual Studio,但不适用于 MinGW:
#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>
void drawText(SDL_Renderer* renderer, TTF_Font* font, int x, int y, const char* text) {
auto surface = TTF_RenderText_Solid(font, text, SDL_Color{ 255, 255, 255, 255 });
auto texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect dstRect = { x, y, surface->w, surface->h };
SDL_RenderCopy(renderer, texture, NULL, &dstRect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("some title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1920, 1080, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
TTF_Init();
bool running = true;
SDL_Event event;
auto font = TTF_OpenFont("assets/courier_prime.ttf", 36);
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
running = false;
}
SDL_SetRenderDrawColor(renderer, 25, 25, 25, 255);
SDL_RenderClear(renderer);
drawText(renderer, font, 10, 10, "Test string");
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255 );
auto rect = SDL_Rect{10, 100, 110, 200};
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);
}
TTF_CloseFont(font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
}
无论我将 SDL_RenderFillRect
放在哪里(在 drawText 之前或之后),我唯一看到的就是文本。矩形不再可见。
我目前正在处理一个混合的 OS 开发环境(mac 和 windows),因此我希望使用 make 在两者的基础上进行构建。
似乎是 SDL_RENDERER_ACCELERATED 和 openGL 未链接的问题。链接 openGL 没有解决问题,但是,切换到软件渲染器 (SDL_RENDERER_SOFTWARE) 确实修复了渲染错误。很奇怪,它确实适用于 VS。我想这仍然是个谜...