关于如何移植在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. Visual Studio中修改项目的输出目录

    1. 如在Solution中的项目名称为 ProjectA 但在本地目录显示却想换成: MyProject 2. 应该做的修改是: 2.1. 将本地目录的 ProjectA手动修改成 MyProjec ...

  2. ubuntu安装甲骨文最新jdk7

    1.下载jdk7(我下载的是Java SE Platform(jdk) 7u51): http://www.oracle.com/technetwork/java/javase/downloads/i ...

  3. [转载+补充][PY3]——环境配置(2)——windows下安装pycharm并连接Linux的python环境

    原文地址:<你所会用到的Python学习环境和工具> 1. 下载安装Pycharm专业版 具体方法略.Pycharm5激活方法参考http://www.cnblogs.com/snsdzj ...

  4. [转]MSBuild Target Framework and Target Platform

    本文转自;https://msdn.microsoft.com/en-us/library/hh264221.aspx A project can be built to run on a targe ...

  5. Opencv中图像的遍历与像素操作

    Opencv中图像的遍历与像素操作 OpenCV中表示图像的数据结构是cv::Mat,Mat对象本质上是一个由数值组成的矩阵.矩阵的每一个元素代表一个像素,对于灰度图像,像素是由8位无符号数来表示(0 ...

  6. vue本地设置请求接口及数据

    1.安装axios yarn add axios 2.在入口文件main.js中设置 import { getRequest, postRequest} from './libs/api';//导入 ...

  7. 数据上下文中的AddOrUpdate方法

    AddOrUpdate是扩展方法,需要添加引用  using System.Data.Entity.Migrations;

  8. inline-block和float的区别,什么时候使用

    文章转载于新浪博客http://blog.sina.com.cn/s/blog_5f39af320101qckt.html 只用于学习交流 什么时候使用inline-block,什么时候使用float ...

  9. 一周一个小demo — vue.js实现备忘录功能

    这个vue实现备忘录的功能demo是K在github上找到的,K觉得这是一个用来对vue.js入门的一个非常简单的demo,所以拿在这里共享一下. (尊重他人劳动成果,从小事做起~  demo原git ...

  10. 关于 ie9 不执行 js 的问题

    当写js用 console.log()调试,但没有注释,在ie 9 浏览器上运行时,console.log()  后面的脚本不能再执行了. 为了ie,记得把console.log() 注释