[原]零基础学习SDL开发之在Android使用SDL2.0显示BMP叠加图
关于如何移植在android上使用SDL,可以参考[原]零基础学习SDL开发之移植SDL2.0到Android 和 [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图 。
在一篇文章我们主要使用SDL2.0来加载一张BMP图来渲染显示,同时叠加一张图作为背景图。
博主的开发环境:Ubuntu 14.04 64位,Eclipse + CDT + ADT+NDK
在前面两篇文章我们知道了如何移植SDL2.0到android上面来,并且可以在Android上面来显示一张图片,这篇文章就是在前两篇文章的基础上来进行继续开发。
一、将功能模块化,将加载BMP的功能封装到一个函数中:
/*
* SDL_Lesson.c
*
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include "SDL.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 *bmp = NULL; /*
* Loads a BMP image into a texture on the rendering device
* @param file The BMP 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;
//Load the image
bmp = SDL_LoadBMP(file); if (bmp == NULL) {
LOGE("SDL_LoadBMP failed %s", SDL_GetError());
}
//If the loading went ok, convert to texture and return the texture
texture = SDL_CreateTextureFromSurface(render, bmp);
SDL_FreeSurface(bmp); if (texture == NULL) {
LOGE("SDL_CreateTextureFromSurface failed %s", SDL_GetError());
} return texture;
}
二、将渲染功能封装到renderTexture函数中:
/*
* SDL_Lesson.c
*
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include "SDL.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 *bmp = NULL; /*
* Loads a BMP image into a texture on the rendering device
* @param file The BMP 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;
//Load the image
bmp = SDL_LoadBMP(file); if (bmp == NULL) {
LOGE("SDL_LoadBMP failed %s", SDL_GetError());
}
//If the loading went ok, convert to texture and return the texture
texture = SDL_CreateTextureFromSurface(render, bmp);
SDL_FreeSurface(bmp); if (texture == NULL) {
LOGE("SDL_CreateTextureFromSurface failed %s", SDL_GetError());
} 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
*/
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y) {
//Setup the destination rectangle to be at the position we want
SDL_Rect dst;
dst.x = x;
dst.y = y;
//Query the texture to get its width and height to use
SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
SDL_RenderCopy(ren, tex, NULL, &dst);
}
三、编写主函数功能:
/*
* SDL_Lesson.c
*
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include "SDL.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 *bmp = NULL; /*
* Loads a BMP image into a texture on the rendering device
* @param file The BMP 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;
//Load the image
bmp = SDL_LoadBMP(file); if (bmp == NULL) {
LOGE("SDL_LoadBMP failed %s", SDL_GetError());
}
//If the loading went ok, convert to texture and return the texture
texture = SDL_CreateTextureFromSurface(render, bmp);
SDL_FreeSurface(bmp); if (texture == NULL) {
LOGE("SDL_CreateTextureFromSurface failed %s", SDL_GetError());
} 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
*/
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y) {
//Setup the destination rectangle to be at the position we want
SDL_Rect dst;
dst.x = x;
dst.y = y;
//Query the texture to get its width and height to use
SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
SDL_RenderCopy(ren, tex, NULL, &dst);
} int main(int argc, char *argv[]) {
char *filefolder = argv[]; char *background_temp = "background.bmp";
char *image_temp = "image.bmp";
LOGI("natvie_SDL %s", filefolder); //char *background_file = "/storage/sdcard0/background.bmp";
char *background_file = (char*) malloc(
strlen(filefolder) + strlen(background_temp) + );
strcpy(background_file, filefolder);
strcat(background_file, background_temp); //char *image_file = "/storage/sdcard0/image.bmp";
char *image_file = (char*) malloc(
strlen(filefolder) + strlen(image_temp) + );
strcpy(image_file, filefolder);
strcat(image_file, image_temp); if (SDL_Init(SDL_INIT_EVERYTHING) != ) {
LOGE("SDL_Init failed %s", SDL_GetError());
} window = SDL_CreateWindow("lesson2", , , SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (window == NULL) {
LOGE("SDL_CreateWindow failed %s", SDL_GetError());
} render = SDL_CreateRenderer(window, -,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (render == NULL) {
LOGE("SDL_CreateRenderer failed %s", SDL_GetError());
} background = loadTexture(background_file, render);
image = loadTexture(image_file, render); //Clear the window
SDL_RenderClear(render); //Get the width and height from the texture so we know how much to move x,y by
//to tile it correctly
int bW, bH;
SDL_QueryTexture(background, NULL, NULL, &bW, &bH);
//We want to tile our background so draw it 4 times
renderTexture(background, render, , );
renderTexture(background, render, bW, );
renderTexture(background, render, , bH);
renderTexture(background, render, bW, bH); //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;
SDL_QueryTexture(image, NULL, NULL, &iW, &iH);
int x = SCREEN_WIDTH / - iW / ;
int y = SCREEN_HEIGHT / - iH / ;
renderTexture(image, render, x, y); //Update the screen
SDL_RenderPresent(render);
SDL_Delay(); cleanup_texture(background);
cleanup_texture(image);
cleanup_render(render);
cleanup_window(window);
SDL_Quit(); return ;
}
四、修改SDLActivity中的 SDLMain类,传入参数为sdcard的路径,其余修改参考上一篇文章,修改内容如下:
/**
Simple nativeInit() runnable
*/
class SDLMain implements Runnable {
@Override
public void run() {
// Runs SDL_main()
String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
SDLActivity.nativeInit(sdcard); //Log.v("SDL", "SDL thread terminated");
}
}
五、运行效果截图

[原]零基础学习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 ... 
- [原]零基础学习SDL开发之在Android使用SDL2.0加载字体
		在上一篇文章我们知道了如何在android使用SDL2.0来渲染显示一张png图,而且在上上一篇我们知道如何使用sdl来渲染输出bmp图,那么sdl是否可以渲染输出自己喜爱的字体库的字体呢?答案是当然 ... 
- [原]零基础学习在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功能看起来是多么赏心悦目. 我们将让左,右箭头来回走在影片中通过一个小的向上和向下箭头很多,其中“三多一 ... 
随机推荐
- orcale 之 初识数据库一
			数据库 数据库顾名思义数据的仓库,只不过这个仓库是在计算机的存储设备之中.一般来说,这些数据面向一个组织,部门或者整个企业,这些数据是按照一定的模型进行存放的数据集合,比如对于一个学生的管理系统来说, ... 
- UML 依赖\泛化\关联\实现\聚合\组合的 Java实现
			在类图中,类与类之间的关系主要有一下几种: 泛化关系:(就是继承) public class Employee { } public class SaleEmployee extends Employ ... 
- zato集群部署
			注: SQL ODB和Cluster’s config需要首先依次创建,其他三个次序随意 对不熟悉的命令,使用server create *** -h 查看帮助文档 修改完后配置文件,要重启(zato ... 
- Yii框架配置语言包
			配置文件frontend\config\main.php 'language' => 'zh-CN', //配置语言包 'i18n' =>[ 'translations' => [ ... 
- 项目开发-->基础功能汇总
			祭奠曾经逝去的青春…… 1.基础功能汇总-->身份认证及用户登录模块 2.基础功能汇总-->一键登录功能汇总 3.堆和栈 4.变量 
- TCP学习(一)
			协议分层 可以看到 物理层, 链路层,网络层是所有网络设备共有的, 而传输层, 会话层, 表示层, 应用层 是存在于主机上的 各设备实现的协议层次 IP地址的表示  为什么会出现ip地址?是为了在一 ... 
- spring mvc随笔
			一.SpringMvc学习笔记1.使用SpringMvc时需在web.xml文件中添加配置 <servlet> <servlet-name>springMVC</serv ... 
- MDI-设置子窗体只能弹出一个--单例模式
			不足之处,欢迎指正! 什么是MDI..我表示不知道的呢. MDI(Multiple Document Interface)就是所谓的多文档界面,与此对应就有单文档界面 (SDI), 它是微软公司从Wi ... 
- shodan在渗透测试中的应用
			场景1:想搜索美国所有的elasticsearch服务器 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.设计 ... 
- hdu 1011  Starship Troopers   经典的树形DP ****
			Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ... 
