从零开始-边学边做-塔防游戏-七彩三国(五)--SDL项目

来源:互联网 发布:舞美设计用什么软件 编辑:程序博客网 时间:2024/05/15 23:52

第四章:SDL项目


SDL应该比cocos2d-x更底层。在学习cocos2d-x之前。想把“七彩三国”的游戏用SDL来简单实现下吧。


首先创建一个项目,我们命名为 SanguoSDL.创建的步骤见上一章。

C:\data\www\SanguoSDL

修改

C:\data\www\SanguoSDL\build.xml

C:\data\www\SanguoSDL\AndroidManifest.xml

中的 “SDLActivity” -> "SanguoSDL"

同时将SDL2,SDL2_image添加到项目中来

新建一个java类,继承SDLAcitivity

C:\data\www\SanguoSDL\src\cn\skypark\SanguoSDL.java

代码如下:

package cn.skypark;import org.libsdl.app.SDLActivity;public class SanguoSDL extends SDLActivity {public SanguoSDL() {// TODO Auto-generated constructor stub}}


cd /cygdrive/c/data/www/SanguoSDL/jnicp /cygdrive/c/data/software/SDL2_image-2.0.0/SDL2_image-2.0.0 SDL_image -rf     cp /cygdrive/c/data/software/SDL2-2.0.1/SDL2-2.0.1 SDL -rfchmod a+wr SDL_image/ -R   chmod a+wr SDL/ -R   


最终目录结构如下:


同时创建了一个打日志的类。从LogCat可以看到,程序已经在模拟器中成功运行。

附上几个主要文件的详细代码:

C:\data\www\SanguoSDL\src\cn\skypark\SanguoSDL.java

package cn.skypark;import org.libsdl.app.SDLActivity;import android.os.Bundle;public class SanguoSDL extends SDLActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}}

C:\data\www\SanguoSDL\jni\skypark_game\SkyparkLog.hpp

#ifndef SKYPARKLOG_HPP_#define SKYPARKLOG_HPP_#include <sys/types.h>#include <time.h>#include <unistd.h>#include <sys/file.h>#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include "SDL_config.h"namespace skypark {class SkyparkLog {public:void log(const char * csformat, ...) {va_list ap;va_start(ap, csformat);log(csformat, ap);va_end(ap);}private:void log(const char * csformat, va_list &ap) {char logBuf[1024 * 4];int iTimeLen = 0;{time_t timep;struct tm *p;time(&timep);p = localtime(&timep);iTimeLen = snprintf(logBuf, sizeof(logBuf) - 1,"[%02d-%02d %02d:%02d:%02d]", (1 + p->tm_mon), p->tm_mday,p->tm_hour, p->tm_min, p->tm_sec);}int len = vsnprintf(logBuf + iTimeLen, sizeof(logBuf) - 2 - iTimeLen,csformat, ap);if (len >= 0 && len < (int) (sizeof(logBuf) - 1 - iTimeLen)) {logBuf[len + iTimeLen] = '\0';SDL_Log("%s\n", logBuf);}}};}#endif


C:\data\www\SanguoSDL\jni\src\Android.mk

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := mainSDL_PATH := ../SDLLOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include# Add your application source files here...LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \SanguoMain.cppLOCAL_SHARED_LIBRARIES := SDL2 SDL2_imageLOCAL_LDLIBS := -lGLESv1_CM -llog -landroidinclude $(BUILD_SHARED_LIBRARY)


C:\data\www\SanguoSDL\jni\src\SanguoMain.cpp

#include <stdlib.h>#include <stdio.h>#include <time.h>#include <dirent.h>#include <jni.h>#include <android/asset_manager.h>#include <android/asset_manager_jni.h>#include "SDL_config.h"#include "SDL_test.h"#include "../skypark_game/SkyparkLog.hpp"using namespace skypark;SkyparkLog g_SkyparkLog;int main(int argc, char *argv[]) {g_SkyparkLog.log("[%s][%d]begin", __FILE__, __LINE__);g_SkyparkLog.log("[%s][%d]end", __FILE__, __LINE__);while (true) {sleep(1);g_SkyparkLog.log("[%s][%d]run...", __FILE__, __LINE__);}return 0;}


C:\data\www\SanguoSDL\AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><!-- Replace org.libsdl.app with the identifier of your game below, e.g.     com.gamemaker.game--><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="cn.skypark"      android:versionCode="1"      android:versionName="1.0"      android:installLocation="auto">    <!-- Create a Java class extending SDLActivity and place it in a         directory under src matching the package, e.g.         src/com/gamemaker/game/MyGame.java         then replace "SDLActivity" with the name of your class (e.g. "MyGame")         in the XML below.         An example Java class can be found in README-android.txt    -->    <application android:label="@string/app_name"                 android:icon="@drawable/ic_launcher"                 android:allowBackup="true"                 android:theme="@android:style/Theme.NoTitleBar.Fullscreen">        <activity android:name="SanguoSDL"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>    <!-- Android 2.3.3 -->    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" />    <!-- OpenGL ES 2.0 -->    <uses-feature android:glEsVersion="0x00020000" />     <!-- Allow writing to external storage -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest> 





0 0
原创粉丝点击