Skip to content

Commit 1d7a25e

Browse files
committed
Added SDL_image support
1 parent cf6d674 commit 1d7a25e

File tree

7 files changed

+28
-11
lines changed

7 files changed

+28
-11
lines changed

CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ set(SOURCE_FILES src/main.cpp)
66
set(SDL_LOCATION ${CMAKE_SOURCE_DIR}/external/SDL2)
77

88
add_library( SDL2 SHARED IMPORTED )
9+
add_library( SDL2_image SHARED IMPORTED )
910
set_target_properties(SDL2 PROPERTIES IMPORTED_LOCATION
1011
${DISTRIBUTION_DIR}/${ANDROID_BUILD_DIR}/obj/local/${ANDROID_ABI}/libSDL2.so)
12+
set_target_properties(SDL2_image PROPERTIES IMPORTED_LOCATION
13+
${DISTRIBUTION_DIR}/${ANDROID_BUILD_DIR}/obj/local/${ANDROID_ABI}/libSDL2_image.so)
1114

12-
include_directories(${SDL_LOCATION}/include)
15+
include_directories(${SDL_LOCATION}/SDL2/include)
16+
include_directories(${SDL_LOCATION}/SDL2_image)
1317

14-
add_library( main SHARED ${SDL_LOCATION}/src/main/android/SDL_android_main.c ${SOURCE_FILES} )
18+
add_library( main SHARED ${SDL_LOCATION}/SDL2/src/main/android/SDL_android_main.c ${SOURCE_FILES} )
1519

16-
target_link_libraries( main SDL2 )
20+
target_link_libraries( main SDL2 SDL2_image )

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Synopsis
44

55
This project is an update of the hello world project here https://github.com/stephen47/android-sdl2-gradle-template, as I couldn't find a good way to integrate a CMake built C++ project to an android application.
6+
It features a sample hello world using SDL and SDL_image.
67

78
## Requirements
89
- JDK and JRE 8
@@ -11,20 +12,24 @@ This project is an update of the hello world project here https://github.com/ste
1112

1213
## Downloading dependencies
1314

14-
Download the latest source release from the SDL website:
15+
Download the latest source release from SDL and SDL_image websites:
1516

1617
https://www.libsdl.org/download-2.0.php
18+
https://www.libsdl.org/projects/SDL_image/
1719

18-
Unzip it, put the SDL2 folder in external and rename it to SDL2 so your project folder should look like this:
20+
Unzip it, put the SDL2-x.x.x and SDL2_image-x.x.x folders in `external/SDL2` and rename them to SDL2 and SDL2_image so your project folder looks like this:
1921
```
2022
+ android
2123
+ external
2224
| + SDL2
25+
| | + Android.mk
26+
| | | SDL2
27+
| | | SDL2_image
2328
```
2429

2530
Then copy the java interface SDLActivity to the android project:
2631
```
27-
cp external/SDL2/android-project/src/org/libsdl/app/SDLActivity.java android/app/src/main/java/org/libsdl/app/
32+
cp external/SDL2/SDL2/android-project/src/org/libsdl/app/SDLActivity.java android/app/src/main/java/org/libsdl/app/
2833
```
2934

3035
There, it's done!

android/app/src/main/assets/res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../res/

android/app/src/main/java/pvallet/com/github/hello_sdl2/HelloSDL2Activity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package pvallet.com.github.hello_sdl2;
22

3-
import org.libsdl.app.SDLActivity;
3+
import org.libsdl.app.SDLActivity;
44

55
public class HelloSDL2Activity extends SDLActivity
66
{
@@ -17,6 +17,7 @@ public class HelloSDL2Activity extends SDLActivity
1717
protected String[] getLibraries() {
1818
return new String[]{
1919
"SDL2",
20+
"SDL2_image",
2021
"main"
2122
};
2223
}

external/SDL2/Android.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include $(call all-subdir-makefiles)

res/hello.png

9.66 KB
Loading

src/main.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <SDL.h>
2+
#include <SDL_image.h>
23
#include <stdio.h>
34

45
int main(int argc, char* argv[]) {
@@ -41,14 +42,18 @@ int main(int argc, char* argv[]) {
4142
SDL_Rect r;
4243
r.x = 50;
4344
r.y = 50;
44-
r.w = 50;
45-
r.h = 50;
45+
r.w = 500;
46+
r.h = 500;
4647

4748
// Set render color to blue ( rect will be rendered in this color )
4849
SDL_SetRenderDrawColor( renderer, 0, 0, 255, 255 );
4950

50-
// Render rect
51-
SDL_RenderFillRect( renderer, &r );
51+
// Render image
52+
SDL_Surface *loadedImage = IMG_Load("res/hello.png");
53+
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, loadedImage);
54+
SDL_FreeSurface(loadedImage);
55+
56+
SDL_RenderCopy(renderer, texture, NULL, &r);
5257

5358
// Render the rect to the screen
5459
SDL_RenderPresent(renderer);

0 commit comments

Comments
 (0)