关于如何移植在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叠加图的更多相关文章

  1. [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图

    关于如何移植SDL2.0到安卓上面来参考我的上一篇文章:[原]零基础学习SDL开发之移植SDL2.0到Android 在一篇文章我们主要使用SDL2.0来加载一张BMP图来渲染显示. 博主的开发环境: ...

  2. [原]零基础学习SDL开发之在Android使用SDL2.0渲染PNG图片

    在上一篇文章我们知道了如何在android使用SDL2.0来渲染显示一张bmp图,但是如果是一张png或者一张jpg的图,那么还能显示成功么?答案是否定的 我们需要移植SDL_image库来支持除bm ...

  3. [原]零基础学习SDL开发之在Android使用SDL2.0加载字体

    在上一篇文章我们知道了如何在android使用SDL2.0来渲染显示一张png图,而且在上上一篇我们知道如何使用sdl来渲染输出bmp图,那么sdl是否可以渲染输出自己喜爱的字体库的字体呢?答案是当然 ...

  4. [原]零基础学习在Android进行SDL开发系列文章

    [原]零基础学习SDL开发之移植SDL2.0到Android [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图 [原]零基础学习SDL开发之在Android使用SDL2.0显示 ...

  5. [原]零基础学习SDL开发之移植SDL2.0到Android

    在[原]SDL开发教程我们知道了如何在pc下使用SDL进行开发,在android上面是否一样可以使用呢?答案是肯定的. 下面我们进行移植SDL到Android,这里都是基于SDL最新版进行移植的,在E ...

  6. [原]零基础学习视频解码之android篇系列文章

    截止今天,<零基础学习视频解码系列文章>.<零基础学习在Android进行SDL开发系列文章>以及<零基础学习视频解码之android篇>系列文章基本算是告一段落了 ...

  7. [原]零基础学习视频解码之安装ffmpeg

    写在文章前面:ffmpeg是一个开源的编解码框架,拥有很强大的功能.但是对于如果使用其来做开发呈现着严重两极分化,大神们讨论着高深的问题,大多数像我这样的小白连门都进不去.最近无意间领会了如何入门,现 ...

  8. [原]零基础学习在Android进行SDL开发后记

    本着学习交流记录的目的编写了这个系列文章,主要用来记录如何从零开始学习SDL开发的过程,在这个过程中遇到了很多问题,差点就放弃了.首先是SDL的Android移植的时候遇到了比较坑的是SDL移植到An ...

  9. [原]零基础学习视频解码之seek

    现在,我们要添加一些功能,当你看不能倒带的电影,是不是很烦? 那么函数av_seek_frame功能看起来是多么赏心悦目. 我们将让左,右箭头来回走在影片中通过一个小的向上和向下箭头很多,其中“三多一 ...

随机推荐

  1. 新时代运维重器 Tencent Hub 最佳实践——云+未来峰会开发者专场回顾

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 演讲者:邹辉 腾讯云 PaaS 产品总监 背景:5月23-24日,以"焕启"为主题的腾讯"云+未来" ...

  2. PyQt5 应用在 TeamViewer 下无法使用全屏模式

    PyQt5 应用在 TeamViewer 下无法使用全屏模式 问题描述 使用 PyQt5 (版本为 5.7)中的 QtWebEngineView 构建的桌面应用,部署到远程机器(Windows 7 平 ...

  3. vue项目中总结用到的方法。

    依赖 vue-router 获得当前字符串,对应当前路由的路径,总是解析为绝对路径. computed: { productIcon () { return this.imgMap[this.$rou ...

  4. 一:SpringMVC架构流程

    架构流程: 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理器映射器. 3.处理器映射器根据请求url ...

  5. mycat核心概念

    一.逻辑库(schema) 业务人员一般是不需要知道数据库中间件的,他们只需要连接到数据库并使用数据库,一切复杂的细节都被中间件给隐藏了,对于业务人员来说中间件即是一个数据库.这里逻辑库的概念就是一个 ...

  6. 比较全的css重设

    一.最简化的CSS Reset(重设) : * { padding:; margin:; } 这是最普遍最简单的CSS重设,将所有元素的padding和margin值都设为0,可以避免一些浏览器在理解 ...

  7. git-中的命令与理解

    改变到要操作仓库的目录创建文件夹(mkdir 文件夹名) git init初始化一个git仓库 git add .git add --all两个命令一样作用,添加目录里面所有文件到本地工作区 git ...

  8. 如何解决织梦DedeCMS后台模块管理列表不显示

    在使用织梦Dedecms的过程中,我们会遇到模块管理列表无法显示的问题,造成织梦模块管理列表无法显示的原因,可能有很多种,现小编总结了遇到过的一种方法仅供参考. 方法步骤一: 由于/data/modu ...

  9. Hibernate (ORM)

    1 框架体系结构 2 hibernate入门 2.1 ORM框架 Hibernate是一个数据持久化层的ORM框架. Object:对象,java对象,此处特指JavaBean Relational: ...

  10. create-react-app找不到配置项

    npm run eject 这个一个不可逆过程,一旦你执行了,就不能回到初始化