[原]零基础学习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 = 640;
const int SCREEN_HEIGHT = 480;

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 = 640;
const int SCREEN_HEIGHT = 480;

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 = 640;
const int SCREEN_HEIGHT = 480;

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[1];

    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) + 1);
    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) + 1);
    strcpy(image_file, filefolder);
    strcat(image_file, image_temp);

    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
        LOGE("SDL_Init failed %s", SDL_GetError());
    }

    window = SDL_CreateWindow("lesson2", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT,
            SDL_WINDOW_SHOWN);
    if (window == NULL) {
        LOGE("SDL_CreateWindow failed %s", SDL_GetError());
    }

    render = SDL_CreateRenderer(window, -1,
            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, 0, 0);
    renderTexture(background, render, bW, 0);
    renderTexture(background, render, 0, 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 / 2 - iW / 2;
    int y = SCREEN_HEIGHT / 2 - iH / 2;
    renderTexture(image, render, x, y);

    //Update the screen
    SDL_RenderPresent(render);
    SDL_Delay(2000);

    cleanup_texture(background);
    cleanup_texture(image);
    cleanup_render(render);
    cleanup_window(window);
    SDL_Quit();

    return 0;
}

四、修改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叠加图,布布扣,bubuko.com

时间: 2024-10-29 19:07:30

[原]零基础学习SDL开发之在Android使用SDL2.0显示BMP叠加图的相关文章

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

关于如何移植SDL2.0到安卓上面来参考我的上一篇文章:[原]零基础学习SDL开发之移植SDL2.0到Android 在一篇文章我们主要使用SDL2.0来加载一张BMP图来渲染显示. 博主的开发环境:Ubuntu 14.04 64位,Eclipse + CDT + ADT+NDK 博主曾经自己使用NDK编译出了libSDL2.so,然后使用共享库的方式来调用libSDL2中的函数,结果发现SDL\src\core\android\SDL_android.c 这个jni函数写的实在是不够自己另外做

[原]零基础学习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拷贝到在上一篇文章中的andro

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

在[原]SDL开发教程我们知道了如何在pc下使用SDL进行开发,在android上面是否一样可以使用呢?答案是肯定的. 下面我们进行移植SDL到Android,这里都是基于SDL最新版进行移植的,在Eclipse中编译. 开发环境:Ubuntu 14.04 64位,Eclipse + CDT + ADT,NDK 一.到官网下载http://www.libsdl.org/hg.php 下载2.0的源码也可以通过hg来clone最新的源码 hg clone http://hg.libsdl.org/

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

[原]零基础学习SDL开发之移植SDL2.0到Android [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图 [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP叠加图 [原]零基础学习SDL开发之在Android使用SDL2.0渲染PNG图片 [原]零基础学习SDL开发之在Android使用SDL2.0加载字体 [原]零基础学习在Android进行SDL开发后记 [原]零基础学习在Android进行SDL开发系列文章,布布扣,bubuko.com

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

本着学习交流记录的目的编写了这个系列文章,主要用来记录如何从零开始学习SDL开发的过程,在这个过程中遇到了很多问题,差点就放弃了.首先是SDL的Android移植的时候遇到了比较坑的是SDL移植到Android的JNI文件编写比较坑,刚开始想着如何将SDL编译成动态共享库,在别的地方直接调用该动态共享库,结果发现死活崩溃在SDL_Init()这个函数,最后发现SDL的JNI文件直接使用SDLActivity的函数,并且JNI采用静态注册的方式.直接在别的地方调用该动态库就跪了,博主能力又有限,又

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

现在,我们要添加一些功能,当你看不能倒带的电影,是不是很烦? 那么函数av_seek_frame功能看起来是多么赏心悦目. 我们将让左,右箭头来回走在影片中通过一个小的向上和向下箭头很多,其中“三多一少”是10秒,“很多”为60秒.因此,我们需要设置我们的主循环,用来捕获击键.然而,当我们得到一个按键,就不能直接称之为函数av_seek_frame.我们所要做的是在我们的主解码循环中,decode_thread循环做相应的处理. 为了检测按键,我们先来看看,看看我们得到了一个SDL_KEYDOW

[原]零基础学习视频解码之后记

嘿嘿,在此之前从来没有用c写个一个简单的demo,最多也是用c写写数据结构练习题什么的,通过这个学习了解了很多以前不了解的东西. 本人比较懒,做什么事情都是只有三分钟热度,但是一直对ffmpeg解码向往而入门不得.<零基础学习视频解码>系列文章严格意义上来说不算是原创,我无非是将按照http://dranger.com/ffmpeg/这个介绍在Ubuntu 14.04上面用Eclipse+CDT插件去实现了一遍,原文中的很多接口在ffmpeg2.3上面变了,有些接口甚至被删除了,这导致大多数情

[原]零基础学习视频解码系列文章

注:本系列文章的开发环境:Ubuntu 14.04+Eclipse4.3.2+CDT+FFmpeg2.3+SDL1.25 [原]零基础学习视频解码之安装ffmpeg [原]零基础学习视频解码之FFMpeg中比较重要的函数以及数据结构 [原]零基础学习视频解码之解码图像 [原]SDL开发教程 [原]零基础学习视频解码之解码声音 [原]零基础学习视频解码之视频线程 [原]零基础学习视频解码之同步视频 [原]零基础学习视频解码之同步音频 [原]零基础学习视频解码之seek [原]零基础学习视频解码之后

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

写在文章前面:ffmpeg是一个开源的编解码框架,拥有很强大的功能.但是对于如果使用其来做开发呈现着严重两极分化,大神们讨论着高深的问题,大多数像我这样的小白连门都进不去.最近无意间领会了如何入门,现在写下这个系列文章<零基础学习视频解码>用来帮大家提供入门基础.博主的开发机器是基于Ubuntu 14.04 64位的,所以这个系列的文章都是在Ubuntu下完成的. 一.安装SDL C语言没有图形库,其中SDL比较小巧,并且是开源的,适合入门者学习.另外安装ffmpeg的时候会检测是否安装SDL