在这篇文章我们主要使用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());
}