Merge branch 'main' into 'sound_effects_game_sdl'

# Conflicts:
#   .vscode/settings.json
#   model.c
This commit is contained in:
Theophyl Hoguet committed 2022-03-21 20:47:47 +00:00
commit 05bd0e2a97
37 files changed
+1943 -37

No files matched your search

+6 -1
View File
@@ -1,4 +1,9 @@
build/
.vscode/
*.txt
!CMakeLists.txt
!CMakeLists.txt
android/bin/*
android/jni/SDL*
android/libs/*
android/obj/*
android/gen/*
+35
View File
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.libsdl.app"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto">
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="23" />
<!-- OpenGL ES 2.0 -->
<uses-feature android:glEsVersion="0x00020000" />
<!-- Allow writing to external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:label="Lightup"
android:icon="@drawable/icon"
android:allowBackup="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:hardwareAccelerated="true" >
<activity android:name="SDLActivity"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
+1
View File
@@ -0,0 +1 @@
../../assets/arial.ttf
+1
View File
@@ -0,0 +1 @@
../../assets/lightbulb_red.png
+1
View File
@@ -0,0 +1 @@
../../assets/lightbulb_white.png
+1
View File
@@ -0,0 +1 @@
../../assets/redo_down.png
+1
View File
@@ -0,0 +1 @@
../../assets/redo_up.png
+1
View File
@@ -0,0 +1 @@
../../assets/restart_down.png
+1
View File
@@ -0,0 +1 @@
../../assets/restart_up.png
+1
View File
@@ -0,0 +1 @@
../../assets/solve_down.png
+1
View File
@@ -0,0 +1 @@
../../assets/solve_up.png
+1
View File
@@ -0,0 +1 @@
../../assets/undo_down.png
+1
View File
@@ -0,0 +1 @@
../../assets/undo_up.png
+11
View File
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="sdl2-demo" default="help">
<property name="target" value="android-23" />
<property environment="env" />
<property name="sdk.dir" value="${env.ANDROID_HOME}" />
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>
+17
View File
@@ -0,0 +1,17 @@
#!/bin/bash
cd jni
wget -c https://www.libsdl.org/release/SDL2-2.0.5.zip
unzip SDL2-2.0.5.zip
mv SDL2-2.0.5 SDL
wget -c https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.14.zip
unzip SDL2_ttf-2.0.14.zip
mv SDL2_ttf-2.0.14 SDL_ttf
wget -c https://www.libsdl.org/projects/SDL_image/release/SDL2_image-2.0.1.zip
unzip SDL2_image-2.0.1.zip
mv SDL2_image-2.0.1 SDL_image
cd ..
+1
View File
@@ -0,0 +1 @@
include $(call all-subdir-makefiles)
+3
View File
@@ -0,0 +1,3 @@
APP_ABI := x86 armeabi-v7a
APP_PLATFORM := android-23
+21
View File
@@ -0,0 +1,21 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := main
SDL_PATH := ../SDL
LOCAL_CFLAGS += -g -std=c99
LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include
YOUR_SRC_FILES= main.c game_aux.c game_ext.c game_private.c game_tools.c game.c history.c model.c
LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c $(YOUR_SRC_FILES)
LOCAL_SHARED_LIBRARIES := SDL2 SDL2_ttf SDL2_image
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
include $(BUILD_SHARED_LIBRARY)
+1
View File
@@ -0,0 +1 @@
../../../game.c
+1
View File
@@ -0,0 +1 @@
../../../game.h
+1
View File
@@ -0,0 +1 @@
../../../game_aux.c
+1
View File
@@ -0,0 +1 @@
../../../game_aux.h
+1
View File
@@ -0,0 +1 @@
../../../game_ext.c
+1
View File
@@ -0,0 +1 @@
../../../game_ext.h
+1
View File
@@ -0,0 +1 @@
../../../game_private.c
+1
View File
@@ -0,0 +1 @@
../../../game_private.h
+1
View File
@@ -0,0 +1 @@
../../../game_tools.c
+1
View File
@@ -0,0 +1 @@
../../../game_tools.h
+1
View File
@@ -0,0 +1 @@
../../../history.c
+1
View File
@@ -0,0 +1 @@
../../../history.h
+1
View File
@@ -0,0 +1 @@
../../../main.c
+1
View File
@@ -0,0 +1 @@
../../../model.c
+1
View File
@@ -0,0 +1 @@
../../../model.h
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -31,7 +31,7 @@ void checkPointer(void* p, char* errMessage) {
void test_output(int i, int excepted, char* message) {
if (i != excepted) {
fprintf(stderr, message);
fprintf(stderr, "%s", message);
exit(EXIT_FAILURE);
}
}
+80 -35
View File
@@ -16,17 +16,17 @@
/* **************************************************************** */
#define RESTART_UP "./restart_up.png"
#define RESTART_DOWN "./restart_down.png"
#define UNDO_UP "./undo_up.png"
#define UNDO_DOWN "./undo_down.png"
#define REDO_UP "./redo_up.png"
#define REDO_DOWN "./redo_down.png"
#define SOLVE_UP "./solve_up.png"
#define SOLVE_DOWN "./solve_down.png"
#define LIGHTBULB_WHITE "./lightbulb_white.png"
#define LIGHTBULB_RED "./lightbulb_red.png"
#define FONT "./arial.ttf"
#define RESTART_UP "restart_up.png"
#define RESTART_DOWN "restart_down.png"
#define UNDO_UP "undo_up.png"
#define UNDO_DOWN "undo_down.png"
#define REDO_UP "redo_up.png"
#define REDO_DOWN "redo_down.png"
#define SOLVE_UP "solve_up.png"
#define SOLVE_DOWN "solve_down.png"
#define LIGHTBULB_WHITE "lightbulb_white.png"
#define LIGHTBULB_RED "lightbulb_red.png"
#define FONT "arial.ttf"
#define LB1 "lb1.mp3"
#define LB2 "lb2.mp3"
#define LB3 "lb3.mp3"
@@ -36,8 +36,34 @@
#define MARK1 "mark1.wav"
#define MARK2 "mark2.wav"
#define MARK3 "mark3.wav"
#define FONTSIZE 200
#ifdef __ANDROID__
static void copy_asset(char* src, char* dst) {
SDL_RWops* file = SDL_RWFromFile(src, "r");
if (!file)
ERROR("%s", "[ERROR] SDL_RWFromFile: %s\n", src);
int size = SDL_RWsize(file);
PRINT("%s", "copy file %s (%d bytes) into %s\n", src, size, dst);
char* buf = (char*)malloc(size + 1);
if (!buf)
ERROR("%s", "[ERROR] malloc\n");
int r = SDL_RWread(file, buf, 1, size);
PRINT("%s", "read %d\n", r);
if (r != size)
ERROR("%s", "[ERROR] fail to read all file (%d bytes)\n", r);
FILE* out = fopen(dst, "w+");
if (!out)
ERROR("%s", "[ERROR] fail to create file %s\n", dst);
int w = fwrite(buf, 1, r, out);
if (r != w)
ERROR("%s", "[ERROR] fail to write all file (%d bytes)\n", w);
fclose(out);
SDL_RWclose(file);
free(buf);
}
#endif
/* **************************************************************** */
struct Env_t {
@@ -71,8 +97,8 @@ struct Env_t {
/* **************************************************************** */
void usage(char* argv[]) {
printf("Usage : %s => Joue la partie par défaut\n", argv[0]);
printf(" %s <filename> => Charge la partie sur le fichier <filename>\n", argv[0]);
PRINT("Usage : %s => Joue la partie par défaut\n", argv[0]);
PRINT(" %s <filename> => Charge la partie sur le fichier <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
@@ -82,10 +108,10 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
env->pressed_undo = false;
env->pressed_redo = false;
env->pressed_solve = false;
printf("To win the game, you must satisfy the following conditions:\n\n");
printf(
"-All non-black squares are lit.\n-No light is lit by another light.\n-Each numbered black square must be orthogonally adjacent to exactly the given "
"number of lights.\n-Non-numbered black squares may have any number of lights adjacent to them.\n");
PRINT("%s", "To win the game, you must satisfy the following conditions:\n\n");
PRINT("%s",
"-All non-black squares are lit.\n-No light is lit by another light.\n-Each numbered black square must be orthogonally adjacent to exactly the given "
"number of lights.\n-Non-numbered black squares may have any number of lights adjacent to them.\n");
if (argc == 1) {
// Create new game
env->g = game_default();
@@ -95,10 +121,17 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
} else {
usage(argv);
}
#ifdef __ANDROID__
const char* dir = SDL_AndroidGetInternalStoragePath();
char font_path[1024];
sprintf(font_path, "%s/%s", dir, FONT);
copy_asset(FONT, font_path);
TTF_Font* font = TTF_OpenFont(font_path, FONTSIZE);
#else
TTF_Font* font = TTF_OpenFont(FONT, FONTSIZE);
#endif
if (!font)
fprintf(stderr, "TTF_OpenFont: %s\n", FONT);
ERROR("TTF_OpenFont: %s\n", FONT);
TTF_SetFontStyle(font, TTF_STYLE_BOLD);
// color of 0 in black wall
SDL_Color color_w = {255, 255, 255, 255};
@@ -108,7 +141,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init zero texture double tab*/
env->zero = malloc(sizeof(SDL_Texture*) * 2);
if (env->zero == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
// if the case has not error
SDL_Surface* surf = TTF_RenderText_Blended(font, "0", color_w);
env->zero[0] = SDL_CreateTextureFromSurface(ren, surf);
@@ -121,7 +154,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init one texture double tab*/
env->one = malloc(sizeof(SDL_Texture*) * 2);
if (env->one == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
// if the case has not error
surf = TTF_RenderText_Blended(font, "1", color_w);
env->one[0] = SDL_CreateTextureFromSurface(ren, surf);
@@ -134,7 +167,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init two texture double tab*/
env->two = malloc(sizeof(SDL_Texture*) * 2);
if (env->two == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
// if the case has not error
surf = TTF_RenderText_Blended(font, "2", color_w);
env->two[0] = SDL_CreateTextureFromSurface(ren, surf);
@@ -147,7 +180,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init three texture double tab*/
env->three = malloc(sizeof(SDL_Texture*) * 2);
if (env->three == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
// if the case has not error
surf = TTF_RenderText_Blended(font, "3", color_w);
env->three[0] = SDL_CreateTextureFromSurface(ren, surf);
@@ -160,7 +193,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init four texture double tab*/
env->four = malloc(sizeof(SDL_Texture*) * 2);
if (env->four == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
// if the case has not error
surf = TTF_RenderText_Blended(font, "4", color_w);
env->four[0] = SDL_CreateTextureFromSurface(ren, surf);
@@ -173,7 +206,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init lightbulb texture double tab*/
env->lightbulb = malloc(sizeof(SDL_Texture*) * 2);
if (env->lightbulb == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
// if the case has not error
env->lightbulb[0] = IMG_LoadTexture(ren, LIGHTBULB_WHITE);
if (!env->lightbulb[0])
@@ -186,7 +219,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init text_restart texture double tab*/
env->text_restart = malloc(sizeof(SDL_Texture*) * 2);
if (env->text_restart == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
// if the case has not error
env->text_restart[0] = IMG_LoadTexture(ren, RESTART_UP);
if (!env->text_restart[0])
@@ -199,7 +232,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init text_undo texture double tab*/
env->text_undo = malloc(sizeof(SDL_Texture*) * 2);
if (env->text_undo == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
// if the case has not error
env->text_undo[0] = IMG_LoadTexture(ren, UNDO_UP);
if (!env->text_undo[0])
@@ -212,7 +245,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init text_redo texture double tab*/
env->text_redo = malloc(sizeof(SDL_Texture*) * 2);
if (env->text_redo == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
// if the case has not error
env->text_redo[0] = IMG_LoadTexture(ren, REDO_UP);
if (!env->text_redo[0])
@@ -225,7 +258,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init text_solve texture double tab*/
env->text_solve = malloc(sizeof(SDL_Texture*) * 2);
if (env->text_solve == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
// if the case has not error
env->text_solve[0] = IMG_LoadTexture(ren, SOLVE_UP);
if (!env->text_solve[0])
@@ -237,23 +270,24 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
env->rec_redo = malloc(sizeof(SDL_Rect));
if (env->rec_redo == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
env->rec_game = malloc(sizeof(SDL_Rect));
if (env->rec_game == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
env->rec_restart = malloc(sizeof(SDL_Rect));
if (env->rec_restart == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
env->rec_solve = malloc(sizeof(SDL_Rect));
if (env->rec_solve == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
env->rec_undo = malloc(sizeof(SDL_Rect));
if (env->rec_undo == NULL)
fprintf(stderr, "NOT ENOUGH MEMORY\n");
ERROR("%s", "NOT ENOUGTH MEMORY\n");
TTF_CloseFont(font);
// Musics lightbulb
@@ -463,6 +497,13 @@ void play_Music(Env* env, bool lightbulb, bool error, bool mark) {
}
}
void ToggleFullscreen(SDL_Window* Window) {
Uint32 FullscreenFlag = SDL_WINDOW_FULLSCREEN;
bool IsFullscreen = SDL_GetWindowFlags(Window) & FullscreenFlag;
SDL_SetWindowFullscreen(Window, IsFullscreen ? 0 : FullscreenFlag);
SDL_ShowCursor(IsFullscreen);
}
bool process(SDL_Window* win, Env* env, SDL_Event* e) {
int w, h;
SDL_GetWindowSize(win, &w, &h);
@@ -534,6 +575,10 @@ bool process(SDL_Window* win, Env* env, SDL_Event* e) {
}
}
}
} else if (e->type == SDL_KEYDOWN) {
const Uint8* state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_F11])
ToggleFullscreen(win);
}
#endif
return false;