SDL_image之hello

来源:互联网 发布:macbook下软件好下吗 编辑:程序博客网 时间:2024/06/09 15:33

在这里我们将之前编译好的SDL_image和SDL等导入jni目录中,

整个目录涉及到vedio和audio的各种资源,待会上传

 

链接http://download.csdn.net/detail/u013571243/9186229

 

由于整个文件很大,所以推荐用eclipse来调试,从r20版本开始ndk的支持就很不错了,跟踪bug比较方便

由于我们不再eclipse中添加头文件的路径,我们只是在android,mk添加,所以eclipse会报错,不过是正常编译的,不过在打包时会进行规则检查会导致整个app无法发布到手机上,这里我们可以

 

下面我们解析代码

 

 

1.首先添加

#define LOGE(format, ...)  __android_log_print(ANDROID_LOG_ERROR,"JNITag",format, ##__VA_ARGS__)#define LOGI(format, ...)  __android_log_print(ANDROID_LOG_INFO, "JNITag",format, ##__VA_ARGS__)

方便调试

 

2.

至于SDL的详细资料,推荐

http://adolfans.github.io/sdltutorialcn/blog/2013/01/28/lesson-3-sdl-extension-libraries/


3.这里我们只介绍

SDL_image的一个函数

SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());

SDL2是只支持BMP格式的图片的,我们这里把SDL2_image移植到案桌了,他支持

BMP,GIF,JPEG,LBM,PCX,PNG,PNM,TGA,TIFF,WEBP,XCF,XPM,XV

我们通过这个函数把图片渲染到屏幕中

#include <stdlib.h>#include <stdio.h>#include <time.h>#include <SDL.h>#include <SDL_image.h>#include <android/log.h>   #include <string>#include <string.h>const int SCREEN_WIDTH  = 480;const int SCREEN_HEIGHT = 800;const int TILE_SIZE = 40;#define LOGE(format, ...)  __android_log_print(ANDROID_LOG_ERROR,"JNITag",format, ##__VA_ARGS__)#define LOGI(format, ...)  __android_log_print(ANDROID_LOG_INFO, "JNITag",format, ##__VA_ARGS__)SDL_Texture* loadTexture(const std::string &file,SDL_Renderer *ren){LOGI("file path: %s",file.c_str());SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());if (texture == NULL){LOGE("error: %s","LoadTexture");}return texture;}void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int w, int h){//Setup the destination rectangle to be at the position we wantSDL_Rect dst;dst.x = x;dst.y = y;dst.w = w;dst.h = h;SDL_RenderCopy(ren, tex, NULL, &dst);}void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y){int w, h;SDL_QueryTexture(tex, NULL, NULL, &w, &h);renderTexture(tex, ren, x, y, w, h);}int main(int argc, char *argv[]){if(SDL_Init(SDL_INIT_VIDEO) != 0){LOGE("error: %s","init video error");return 1;}SDL_Window *window = SDL_CreateWindow("Lesson 3", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);if (window == NULL){LOGE("error: %s","CreateWindow");SDL_Quit();return 1;}SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);if (renderer == NULL){LOGE("error: %s","CreateRenderer");SDL_Quit();return 1;}SDL_Texture *background = loadTexture("/storage/sdcard0/background.png", renderer);SDL_Texture *image = loadTexture("/storage/sdcard0/image.png", renderer);//Make sure they both loaded okif (background == NULL || image == NULL){SDL_Quit();return 1;}//A sleepy rendering loop, wait for 3 seconds and render and present the screen each timefor (int i = 0; i < 3; ++i){//Clear the windowSDL_RenderClear(renderer);//Determine how many tiles we'll need to fill the screenint xTiles = SCREEN_WIDTH / TILE_SIZE;int yTiles = SCREEN_HEIGHT / TILE_SIZE;//Draw the tiles by calculating their positionsfor (int i = 0; i < xTiles * yTiles; ++i){int x = i % xTiles;int y = i / xTiles;renderTexture(background, renderer, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);}//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 centeredint 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, renderer, x, y);//Update the screenSDL_RenderPresent(renderer);//Take a quick break after all that hard workSDL_Delay(1000);}//Destroy the various itemsSDL_DestroyWindow(window);SDL_DestroyRenderer(renderer);SDL_DestroyTexture(background);SDL_DestroyTexture(image);IMG_Quit();SDL_Quit();    exit(0);}


效果

 




 

0 0
原创粉丝点击