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

我们需要移植SDL_image库来支持除bmp之外的图片格式。

一、移植SDL_image库:

使用如下命令,从SDL Mercurial获取SDL_image的源码:

hg clone https://hg.libsdl.org/SDL_image/

将SDL_image拷贝到在上一篇文章中的android-project\jni\下,将平台相关的代码去掉以及自动自动化相关的文件去掉,保留android相关的代码与文件。

二、在android添加相关的引用:

修改android-project\jni\src\Android.mk,添加相关的引用:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := main

SDL_PATH := ../SDL
SDL_IMAGE_PATH := ../SDL2_image LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include \
$(LOCAL_PATH)/$(SDL_IMAGE_PATH) # Add your application source files here...
LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \
SDL_lesson.c LOCAL_SHARED_LIBRARIES := SDL2
LOCAL_SHARED_LIBRARIES += SDL2_image LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog include $(BUILD_SHARED_LIBRARY)

三、代码中引用相关的头文件:

/*
* SDL_lesson.c
* Clipping Sprite Sheets
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include "SDL.h"
#include "SDL_image.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;

四、加载图片的部分修改为IMG_LoadTexture:

/*
* SDL_lesson.c
* Clipping Sprite Sheets
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include "SDL.h"
#include "SDL_image.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; /*
* Loads a image into a texture on the rendering device
* @param file The 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; texture = IMG_LoadTexture(render, file); if (texture == NULL) {
LOGE("SDL_CreateTextureFromSurface failed %s", SDL_GetError());
} else {
LOGI("SDL_CreateTextureFromSurface successful.");
} return texture;
}

五、编写主函数:

/*
* SDL_lesson.c
* Clipping Sprite Sheets
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include "SDL.h"
#include "SDL_image.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; /*
* Loads a image into a texture on the rendering device
* @param file The 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; texture = IMG_LoadTexture(render, file); if (texture == NULL) {
LOGE("SDL_CreateTextureFromSurface failed %s", SDL_GetError());
} else {
LOGI("SDL_CreateTextureFromSurface successful.");
} 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
* @param w The width of the texture to draw
* @param h The height of the texture to draw
*/
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, SDL_Rect dst,
SDL_Rect *clip) {
SDL_RenderCopy(ren, tex, clip, &dst);
} /*
* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
* the texture's width and height and taking a clip of the texture if desired
* If a clip is passed, the clip's width and height will be used instead of the texture's
* @param tex The source texture we want to draw
* @param rend The renderer we want to draw too
* @param x The x coordinate to draw too
* @param y The y coordinate to draw too
* @param clip The sub-section of the texture to draw (clipping rect)
* default of nullptr draws the entire texture
*/
void renderTexture2(SDL_Texture *tex, SDL_Renderer *ren, int x, int y,
SDL_Rect *clip) {
SDL_Rect dst;
dst.x = x;
dst.y = y;
if (clip != NULL) {
dst.w = clip->w;
dst.h = clip->h;
} else {
SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
}
renderTexture(tex, ren, dst, clip);
} int main(int argc, char *argv[]) {
//char *filefolder = "/storage/sdcard0/";
char *filefolder = argv[1];
char *image_temp = "image.png";
char *background_temp = "background.png";
LOGI("natvie_SDL %s", filefolder); char *background_file = (char*) malloc(
strlen(filefolder) + strlen(background_temp) + 1);
strcpy(background_file, filefolder);
strcat(background_file, background_temp); char *image_file = (char*) malloc(
strlen(filefolder) + strlen(image_temp) + 1);
strcpy(image_file, filefolder);
strcat(image_file, image_temp);
LOGI("natvie_SDL %s", image_file); if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
LOGE("SDL_Init failed %s", SDL_GetError());
} window = SDL_CreateWindow("lesson5", 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); //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 = 100, iH = 100;
int x = SCREEN_WIDTH / 2 - iW / 2;
int y = SCREEN_HEIGHT / 2 - iH / 2; //Setup the clips for our image
SDL_Rect clips[4]; int i;
for (i = 0; i < 4; i++) {
clips[i].x = i / 2 * iW;
clips[i].y = i % 2 * iH;
clips[i].w = iW;
clips[i].h = iH;
} //Specify a default clip to start with
int useClip = 0; SDL_Event e;
int quit = 1;
while (quit != 0) {
//Read any events that occured, for now we'll just quit if any event occurs
while (SDL_PollEvent(&e)) {
//If user closes the window
if (e.type == SDL_QUIT) {
quit = 0;
}
//If user presses any key
if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_1:
useClip = 0;
break; case SDLK_2:
useClip = 1;
break; case SDLK_3:
useClip = 2;
break; case SDLK_4:
useClip = 3;
break; case SDLK_ESCAPE:
quit = 0;
break; default:
break;
}
}
//If user clicks the mouse
if (e.type == SDL_MOUSEBUTTONDOWN) {
quit = 0;
}
} //Rendering
SDL_RenderClear(render); //We want to tile our background so draw it 4 times
renderTexture2(background, render, 0, 0, NULL);
renderTexture2(background, render, bW, 0, NULL);
renderTexture2(background, render, 0, bH, NULL);
renderTexture2(background, render, bW, bH, NULL); //Draw the image
renderTexture2(image, render, x, y, &clips[useClip]);
SDL_RenderPresent(render);
}
//Destroy the various items
cleanup_texture(image);
cleanup_render(render);
cleanup_window(window);
IMG_Quit();
SDL_Quit(); return 0;
}

六、修改SDLActivity,加载SDL2_image库:

  // Load the .so
static {
System.loadLibrary("SDL2");
System.loadLibrary("SDL2_image");
//System.loadLibrary("SDL2_mixer");
//System.loadLibrary("SDL2_net");
//System.loadLibrary("SDL2_ttf");
System.loadLibrary("main");
}

运行截图:

[原]零基础学习SDL开发之在Android使用SDL2.0渲染PNG图片的更多相关文章

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

    关于如何移植在android上使用SDL,可以参考[原]零基础学习SDL开发之移植SDL2.0到Android 和 [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图 . 在一篇 ...

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

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

  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. 转 Unity企业级支持案例与分析

    Unity大中华区技术支持总监张黎明以“Unity企业级支持案例与分析”为主题进行了分享. 以下为演讲实录: 张黎明:非常感谢大家来参加今年的Unite,其实我现在看到有的朋友已经不是第一次来参加Un ...

  2. 从外网GitHub clone开源项目的时候,.git文件过大,导致克隆慢

    以clone impala为例,主要是加入-depth=1参数: git clone -b cdh4-2.0 --depth=1 https://github.com/cloudera/Impala. ...

  3. Codeforces 494E. Sharti

    Description 有一个 \(n*n\) 的矩形,给出 \(m\) 个子矩形,这些矩形内部的点都是白色的,其余的点都是黑色,每一次你可以选择一个变长不超过 \(k\) 的正方形,满足这个正方形的 ...

  4. 阿里Java开发电话面试经历--惨败

    近期准备跳槽,想试试知名大企业--阿里.经过boss直聘上一些内部人员的内推,有幸获得了一次电话面试的机会.(虽然在面试开始之前就大概知道结果是如何,但是也总得试试自己个有多水,哈哈哈...) 跟大家 ...

  5. Python基础学习总结(六)

    8.函数 函数是带名字的代码块,用于完成具体的工作.def函数定义,指出函数名.定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参 ...

  6. 很多个java面试题

    1. 为什么说Java是一门平台无关语言? 平台无关实际的含义是“一次编写到处运行”.Java 能够做到是因为它的字节码(byte code)可以运行在任何操作系统上,与底层系统无关. 2. 为什么 ...

  7. redis的持久化方式

    redis有两种持久化方式,第一种是基于快照的持久化方式,第二种是基于文件追加的持久化方式 一.基于快照的持久化 1.修改redis.conf配置文件,开启基于快照的持久化方式 2.修改持久化文件存放 ...

  8. 卸载或安装程序出现:The feature you are trying to use is on a network resource ...

    卸载或安装程序出现:The feature you are trying to use is on a network resource ... 这种情况可能是因为原先已经安装过这个软件,所以要先卸载 ...

  9. Java测试工具使用(1)--Junit

    在进行测试之前需要导入junit的两个包,分别是 junit:4.12;hamcrest-core:1.1 1.基本测试标签 @Test.@Before.@After 2.组测试 有时候多个测试文件, ...

  10. 详细解释什么是JavaEE?

    也许你学习了那么久的Java了,但如果有人问你什么是JavaEE?你会怎么回答他呢?在此我来谈谈关于JavaEE的相关技术.(仅是个人见解) 在谈JavaEE时,我们首先来了解一下Java平台.目前, ...