[原]零基础学习SDL开发之在Android使用SDL2.0加载字体
在上一篇文章我们知道了如何在android使用SDL2.0来渲染显示一张png图,而且在上上一篇我们知道如何使用sdl来渲染输出bmp图,那么sdl是否可以渲染输出自己喜爱的字体库的字体呢?答案是当然可以。
我们需要移植SDL_ttf字体库来支持相应的字体的渲染输出。
一、移植SDL_ttf库:
使用如下命令,从SDL Mercurial获取SDL_image的源码:
hg clone https://hg.libsdl.org/SDL_ttf/
将SDL_ttf拷贝到在上一篇文章中的android-project\jni\下,将平台相关的代码去掉以及自动自动化相关的文件去掉,保留android相关的代码与文件。
二、在android中添加相关的引用:
修改android-project\jni\src\Android.mk,添加相关的引用:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := main SDL_PATH := ../SDL
SDL_IMAGE_PATH := ../SDL2_image
SDL_TTF_PATH := ../SDL_ttf LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/$(SDL_IMAGE_PATH)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/$(SDL_TTF_PATH) # Add your application source files here...
LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \
SDL_lesson.c LOCAL_SHARED_LIBRARIES := SDL2
LOCAL_SHARED_LIBRARIES += SDL2_image
LOCAL_SHARED_LIBRARIES += SDL2_ttf LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog include $(BUILD_SHARED_LIBRARY)
三、代码中引用相关的头文件:
/*
* SDL_lesson.c
* Clipping Sprite Sheets
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_logger.h"
#include "SDL_main.h"
#include "SDL_cleanup.h" //The attributes of the screen
const int SCREEN_WIDTH = ;
const int SCREEN_HEIGHT = ; struct SDL_Window *window = NULL;
struct SDL_Renderer *render = NULL; struct SDL_Texture *background = NULL;
struct SDL_Texture *image = NULL; struct SDL_Surface *surface = NULL;
struct SDL_Texture *texture = NULL;
四、加载图片的部分修改为IMG_LoadTexture:
* SDL_lesson.c
* Clipping Sprite Sheets
* Created on: Aug ,
* Author: clarck
*/
#include <jni.h>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_logger.h"
#include "SDL_main.h"
#include "SDL_cleanup.h" //The attributes of the screen
const int SCREEN_WIDTH = ;
const int SCREEN_HEIGHT = ; struct SDL_Window *window = NULL;
struct SDL_Renderer *render = NULL; struct SDL_Texture *background = NULL;
struct SDL_Texture *image = NULL; struct SDL_Surface *surface = NULL;
struct SDL_Texture *texture = NULL; /*
* Loads a image into a texture on the rendering device
* @param file The image file to load
* @param ren The renderer to load the texture onto
* @return the loaded texture, or NULL if something went wrong.
*/
SDL_Texture* loadTexture(const char *file, SDL_Renderer *render) {
struct SDL_Texture *texture = NULL; texture = IMG_LoadTexture(render, file); if (texture == NULL) {
LOGE("SDL_CreateTextureFromSurface failed %s", SDL_GetError());
} else {
LOGI("SDL_CreateTextureFromSurface successful.");
} return texture;
} /*
* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
* the texture's width and height
* @param tex The source texture we want to draw
* @param ren The renderer we want to draw too
* @param x The x coordinate to draw too
* @param y The y coordinate to draw too
* @param w The width of the texture to draw
* @param h The height of the texture to draw
*/
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, SDL_Rect dst,
SDL_Rect *clip) {
SDL_RenderCopy(ren, tex, clip, &dst);
} /*
* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
* the texture's width and height and taking a clip of the texture if desired
* If a clip is passed, the clip's width and height will be used instead of the texture's
* @param tex The source texture we want to draw
* @param rend The renderer we want to draw too
* @param x The x coordinate to draw too
* @param y The y coordinate to draw too
* @param clip The sub-section of the texture to draw (clipping rect)
* default of nullptr draws the entire texture
*/
void renderTexture2(SDL_Texture *tex, SDL_Renderer *ren, int x, int y,
SDL_Rect *clip) {
SDL_Rect dst;
dst.x = x;
dst.y = y;
if (clip != NULL) {
dst.w = clip->w;
dst.h = clip->h;
} else {
SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
}
renderTexture(tex, ren, dst, clip);
}
五、添加字体渲染功能函数:
/*
* SDL_lesson.c
* Clipping Sprite Sheets
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_logger.h"
#include "SDL_main.h"
#include "SDL_cleanup.h" //The attributes of the screen
const int SCREEN_WIDTH = ;
const int SCREEN_HEIGHT = ; struct SDL_Window *window = NULL;
struct SDL_Renderer *render = NULL; struct SDL_Texture *background = NULL;
struct SDL_Texture *image = NULL; struct SDL_Surface *surface = NULL;
struct SDL_Texture *texture = NULL; /*
* Loads a image into a texture on the rendering device
* @param file The image file to load
* @param ren The renderer to load the texture onto
* @return the loaded texture, or NULL if something went wrong.
*/
SDL_Texture* loadTexture(const char *file, SDL_Renderer *render) {
struct SDL_Texture *texture = NULL; texture = IMG_LoadTexture(render, file); if (texture == NULL) {
LOGE("SDL_CreateTextureFromSurface failed %s", SDL_GetError());
} else {
LOGI("SDL_CreateTextureFromSurface successful.");
} return texture;
} /*
* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
* the texture's width and height
* @param tex The source texture we want to draw
* @param ren The renderer we want to draw too
* @param x The x coordinate to draw too
* @param y The y coordinate to draw too
* @param w The width of the texture to draw
* @param h The height of the texture to draw
*/
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, SDL_Rect dst,
SDL_Rect *clip) {
SDL_RenderCopy(ren, tex, clip, &dst);
} /*
* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
* the texture's width and height and taking a clip of the texture if desired
* If a clip is passed, the clip's width and height will be used instead of the texture's
* @param tex The source texture we want to draw
* @param rend The renderer we want to draw too
* @param x The x coordinate to draw too
* @param y The y coordinate to draw too
* @param clip The sub-section of the texture to draw (clipping rect)
* default of nullptr draws the entire texture
*/
void renderTexture2(SDL_Texture *tex, SDL_Renderer *ren, int x, int y,
SDL_Rect *clip) {
SDL_Rect dst;
dst.x = x;
dst.y = y;
if (clip != NULL) {
dst.w = clip->w;
dst.h = clip->h;
} else {
SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
}
renderTexture(tex, ren, dst, clip);
} SDL_Texture* renderText(char *message, char *fontFile, SDL_Color color,
int fontSize, SDL_Renderer *renderer) {
//Open the font
TTF_Font *font = TTF_OpenFont(fontFile, fontSize); if (font == NULL) {
LOGE("renderText error: %s", SDL_GetError());
return NULL;
} surface = TTF_RenderText_Blended(font, message, color);
if (surface == NULL) {
TTF_CloseFont(font);
LOGE("TTF_RenderText_Blended error: %s", SDL_GetError());
return NULL;
} texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture == NULL) {
LOGE("SDL_CreateTextureFromSurface error: %s", SDL_GetError());
} //Clean up the surface and font
SDL_FreeSurface(surface);
TTF_CloseFont(font);
return texture;
}
六、编写主函数功能:
/*
* SDL_lesson.c
* Clipping Sprite Sheets
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_logger.h"
#include "SDL_main.h"
#include "SDL_cleanup.h" //The attributes of the screen
const int SCREEN_WIDTH = ;
const int SCREEN_HEIGHT = ; struct SDL_Window *window = NULL;
struct SDL_Renderer *render = NULL; struct SDL_Texture *background = NULL;
struct SDL_Texture *image = NULL; struct SDL_Surface *surface = NULL;
struct SDL_Texture *texture = NULL; /*
* Loads a image into a texture on the rendering device
* @param file The image file to load
* @param ren The renderer to load the texture onto
* @return the loaded texture, or NULL if something went wrong.
*/
SDL_Texture* loadTexture(const char *file, SDL_Renderer *render) {
struct SDL_Texture *texture = NULL; texture = IMG_LoadTexture(render, file); if (texture == NULL) {
LOGE("SDL_CreateTextureFromSurface failed %s", SDL_GetError());
} else {
LOGI("SDL_CreateTextureFromSurface successful.");
} return texture;
} /*
* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
* the texture's width and height
* @param tex The source texture we want to draw
* @param ren The renderer we want to draw too
* @param x The x coordinate to draw too
* @param y The y coordinate to draw too
* @param w The width of the texture to draw
* @param h The height of the texture to draw
*/
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, SDL_Rect dst,
SDL_Rect *clip) {
SDL_RenderCopy(ren, tex, clip, &dst);
} /*
* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
* the texture's width and height and taking a clip of the texture if desired
* If a clip is passed, the clip's width and height will be used instead of the texture's
* @param tex The source texture we want to draw
* @param rend The renderer we want to draw too
* @param x The x coordinate to draw too
* @param y The y coordinate to draw too
* @param clip The sub-section of the texture to draw (clipping rect)
* default of nullptr draws the entire texture
*/
void renderTexture2(SDL_Texture *tex, SDL_Renderer *ren, int x, int y,
SDL_Rect *clip) {
SDL_Rect dst;
dst.x = x;
dst.y = y;
if (clip != NULL) {
dst.w = clip->w;
dst.h = clip->h;
} else {
SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
}
renderTexture(tex, ren, dst, clip);
} SDL_Texture* renderText(char *message, char *fontFile, SDL_Color color,
int fontSize, SDL_Renderer *renderer) {
//Open the font
TTF_Font *font = TTF_OpenFont(fontFile, fontSize); if (font == NULL) {
LOGE("renderText error: %s", SDL_GetError());
return NULL;
} surface = TTF_RenderText_Blended(font, message, color);
if (surface == NULL) {
TTF_CloseFont(font);
LOGE("TTF_RenderText_Blended error: %s", SDL_GetError());
return NULL;
} texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture == NULL) {
LOGE("SDL_CreateTextureFromSurface error: %s", SDL_GetError());
} //Clean up the surface and font
SDL_FreeSurface(surface);
TTF_CloseFont(font);
return texture;
} int main(int argc, char *argv[]) {
//char *filefolder = "/storage/sdcard0/";
char *filefolder = argv[]; char *font_temp = "sample.ttf";
char *image_temp = "image.png";
char *background_temp = "background.png";
LOGI("natvie_SDL %s", filefolder); char *background_file = (char*) malloc(
strlen(filefolder) + strlen(background_temp) + );
strcpy(background_file, filefolder);
strcat(background_file, background_temp);
LOGI("natvie_SDL background_file : %s", background_file); char *image_file = (char*) malloc(
strlen(filefolder) + strlen(image_temp) + );
strcpy(image_file, filefolder);
strcat(image_file, image_temp);
LOGI("natvie_SDL image_file : %s", image_file); char *font_file = (char*) malloc(
strlen(filefolder) + strlen(font_temp) + );
strcpy(font_file, filefolder);
strcat(font_file, font_temp);
LOGI("natvie_SDL font_file : %s", font_file); if (SDL_Init(SDL_INIT_EVERYTHING) != ) {
LOGE("SDL_Init failed %s", SDL_GetError());
return ;
} if (TTF_Init() != ) {
LOGE("TTF_Init failed %s", SDL_GetError());
SDL_Quit();
return ;
} window = SDL_CreateWindow("lesson5", , , SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (window == NULL) {
LOGE("SDL_CreateWindow failed %s", SDL_GetError());
TTF_Quit();
SDL_Quit();
return ;
} render = SDL_CreateRenderer(window, -,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (render == NULL) {
LOGE("SDL_CreateRenderer failed %s", SDL_GetError());
cleanup_window(window);
TTF_Quit();
SDL_Quit();
return ;
} SDL_Color color = { , , , }; image = renderText("TTF fonts are cool !", font_file, color, , render); if (image == NULL) {
cleanup_texture(image);
cleanup_render(render);
cleanup_window(window);
TTF_Quit();
SDL_Quit();
return ;
} //Clear the window
SDL_RenderClear(render); //Draw our image in the center of the window
//We need the foreground image's width to properly compute the position
//of it's top left corner so that the image will be centered
int iW, iH;
int x = SCREEN_WIDTH / - iW / ;
int y = SCREEN_HEIGHT / - iH / ; SDL_Event e;
int quit = ;
while (quit != ) {
//Read any events that occured, for now we'll just quit if any event occurs
while (SDL_PollEvent(&e)) {
//If user closes the window
if (e.type == SDL_QUIT) {
quit = ;
}
if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE) {
quit = ;
}
} //Rendering
SDL_RenderClear(render); //Draw the image
renderTexture2(image, render, x, y, NULL);
SDL_RenderPresent(render);
}
//Destroy the various items
cleanup_texture(image);
cleanup_render(render);
cleanup_window(window);
IMG_Quit();
SDL_Quit(); return ;
}
七、修改SDLActivity,添加SDL_ttf动态库的load:
// Load the .so
static {
System.loadLibrary("SDL2");
System.loadLibrary("SDL2_image");
//System.loadLibrary("SDL2_mixer");
//System.loadLibrary("SDL2_net");
System.loadLibrary("SDL2_ttf");
System.loadLibrary("main");
}
运行截图:

[原]零基础学习SDL开发之在Android使用SDL2.0加载字体的更多相关文章
- [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP叠加图
关于如何移植在android上使用SDL,可以参考[原]零基础学习SDL开发之移植SDL2.0到Android 和 [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图 . 在一篇 ...
- [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图
关于如何移植SDL2.0到安卓上面来参考我的上一篇文章:[原]零基础学习SDL开发之移植SDL2.0到Android 在一篇文章我们主要使用SDL2.0来加载一张BMP图来渲染显示. 博主的开发环境: ...
- [原]零基础学习SDL开发之在Android使用SDL2.0渲染PNG图片
在上一篇文章我们知道了如何在android使用SDL2.0来渲染显示一张bmp图,但是如果是一张png或者一张jpg的图,那么还能显示成功么?答案是否定的 我们需要移植SDL_image库来支持除bm ...
- [原]零基础学习在Android进行SDL开发系列文章
[原]零基础学习SDL开发之移植SDL2.0到Android [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图 [原]零基础学习SDL开发之在Android使用SDL2.0显示 ...
- [原]零基础学习SDL开发之移植SDL2.0到Android
在[原]SDL开发教程我们知道了如何在pc下使用SDL进行开发,在android上面是否一样可以使用呢?答案是肯定的. 下面我们进行移植SDL到Android,这里都是基于SDL最新版进行移植的,在E ...
- [原]零基础学习视频解码之android篇系列文章
截止今天,<零基础学习视频解码系列文章>.<零基础学习在Android进行SDL开发系列文章>以及<零基础学习视频解码之android篇>系列文章基本算是告一段落了 ...
- [原]零基础学习视频解码之安装ffmpeg
写在文章前面:ffmpeg是一个开源的编解码框架,拥有很强大的功能.但是对于如果使用其来做开发呈现着严重两极分化,大神们讨论着高深的问题,大多数像我这样的小白连门都进不去.最近无意间领会了如何入门,现 ...
- [原]零基础学习在Android进行SDL开发后记
本着学习交流记录的目的编写了这个系列文章,主要用来记录如何从零开始学习SDL开发的过程,在这个过程中遇到了很多问题,差点就放弃了.首先是SDL的Android移植的时候遇到了比较坑的是SDL移植到An ...
- [原]零基础学习视频解码之seek
现在,我们要添加一些功能,当你看不能倒带的电影,是不是很烦? 那么函数av_seek_frame功能看起来是多么赏心悦目. 我们将让左,右箭头来回走在影片中通过一个小的向上和向下箭头很多,其中“三多一 ...
随机推荐
- nexus开机启动
在/etc/init.d目录下创建nexus文件 #!/bin/bash #chkconfig: #description:nexus3 #processname:nexus3 export JAVA ...
- 腾讯云AI平台张文杰:构建一站式机器学习服务平台
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 5月24日,以"无界数据无限智能"为主题的腾讯"云+未来"峰会AI大数据分论坛在广州拉开帷幕.此次分 ...
- Hibernate 一对多自身关联(同一表中子父目录树形结构)
- python3根据地址批量获取百度地图经纬度
python3代码如下: import requests import time def get_mercator(addr): url= 'http://api.map.baidu.com/geoc ...
- jQuery实现单击和鼠标感应事件。
1.实现单击事件动态交替http://www.cnblogs.com/ahthw/p/4232837.html讲到了toggleClass(),对于单击事件而言,jQuery同样提供了动态交替的tog ...
- Java - 线程封闭
保证并发安全性的方式有三: 不共享.不可变.同步 前两种方式相对第三种要简单一些. 这一篇不说语言特性和API提供的相关同步机制,主要记录一下关于共享的一些思考. 共享(shared),可以简单地认为 ...
- 使用connect-multiparty限制nodejs图片上传
connect-multiparty中间件,可用于获取文件上传时各种参数,比如文件大小.格式等,具体使用: var multipart = require('connect-multiparty'); ...
- Newtonsoft.Json 全部配置
需要在序列化时候,忽略掉某些字段,对Newtonsoft.Json进行全局配置,如下: 1. 自定 ContractResolver public class MyContractResolver : ...
- Java API 之 动态代理
一.代理模式 如图:由于某些原因我们希望对该实现类进行访问控制.功能增强等,那么加入一层代理层,用代理层来调用实现类是一个很好的方式来解决这个问题,我们可以在调用实现类功能前后进行校验或者加入一些功能 ...
- javaEE Design Patter(2)详解3个设计模式
Factory (bean creating) Proxy(Agent)(Aop)书有~ Templete(springMVC Facelets)//此处及以上忽略 工厂模式(Factory) 工厂模 ...