diff --git a/Makefile b/Makefile index a6d09828d..e76899c3f 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,7 @@ powder.exe: build/powder.exe powder-release: build/powder-release powder: build/powder powder-x: build/powder-x +powder-x.jnilib: build/powder-x.jnilib build/powder-release.exe: CFLAGS += -DWIN32 -O3 -ftree-vectorize -msse2 -funsafe-math-optimizations -ffast-math -fomit-frame-pointer -funsafe-loop-optimizations -Wunsafe-loop-optimizations build/powder-release.exe: LFLAGS := -lmingw32 -lregex -lws2_32 -lSDLmain -lpthread -lSDL -lm -lbz2 -llua -lfftw3f-3 -mwindows @@ -31,6 +32,8 @@ build/powder: LFLAGS := -lSDL -lm -lbz2 -llua -lfftw3f #build/powder-x: LFLAGS := -lm -lbz2 -lfftw3f -framework SDL -framework Lua -framework Cocoa -framework OpenGL build/powder-x: CFLAGS += -DMACOSX -I/Library/Frameworks/SDL.framework/Headers -I/Library/Frameworks/Lua.framework/Headers -DPIX32BGRA build/powder-x: LFLAGS := -lm -lbz2 -lfftw3f -framework SDL -framework Lua -framework Cocoa +build/powder-x.jnilib: CFLAGS += -DMACOSX -DUSE_JNI -I/Library/Frameworks/Lua.framework/Headers -I/System/Library/Frameworks/JavaVM.framework/Headers -DOGLR -DPIX32OGL -DPIXALPHA +build/powder-x.jnilib: LFLAGS := -lm -lbz2 -lfftw3f -framework Lua -framework JavaVM -framework Cocoa -framework OpenGL CFLAGS += -DGRAVFFT -DLUACONSOLE @@ -67,6 +70,14 @@ build/obj/powder-x/%.o: src/%.cpp $(HEADERS) buildpaths-powder-x: $(shell mkdir -p build/obj/powder-x/) $(shell mkdir -p $(sort $(dir $(patsubst build/obj/%.o,build/obj/powder-x/%.o,$(OBJS))))) + +build/powder-x.jnilib: buildpaths-powder-x.jnilib generate $(patsubst build/obj/%.o,build/obj/powder-x.jnilib/%.o,$(OBJS)) + $(CPPC) -dynamiclib $(CFLAGS) $(OFLAGS) $(LDFLAGS) $(patsubst build/obj/%.o,build/obj/powder-x.jnilib/%.o,$(OBJS)) src/powdertoyjava/OpenGLCanvasMacOS.mm $(LFLAGS) -o $@ -ggdb +build/obj/powder-x.jnilib/%.o: src/%.cpp $(HEADERS) + $(CPPC) -c $(CFLAGS) $(OFLAGS) -o $@ $< -ggdb +buildpaths-powder-x.jnilib: + $(shell mkdir -p build/obj/powder-x.jnilib/) + $(shell mkdir -p $(sort $(dir $(patsubst build/obj/%.o,build/obj/powder-x.jnilib/%.o,$(OBJS))))) generate: $(GENERATEDSOURCES) touch generate diff --git a/src/Graphics.cpp b/src/Graphics.cpp index 36c271d85..e05f3fc2d 100644 --- a/src/Graphics.cpp +++ b/src/Graphics.cpp @@ -1,5 +1,4 @@ #include -#include "SDL.h" #include #include #include "Config.h" diff --git a/src/Graphics.h b/src/Graphics.h index f404d40cc..ab415a443 100644 --- a/src/Graphics.h +++ b/src/Graphics.h @@ -1,7 +1,6 @@ #ifndef GRAPHICS_H #define GRAPHICS_H -#include "SDL.h" #include #if defined(OGLR) #include "OpenGLHeaders.h" diff --git a/src/cat/CommandInterface.h b/src/cat/CommandInterface.h index 2da30c231..e0d680c4d 100644 --- a/src/cat/CommandInterface.h +++ b/src/cat/CommandInterface.h @@ -9,7 +9,7 @@ #define KITTY_H_ #include -#include "SDL.h" +#include "interface/Engine.h" //#include "game/GameModel.h" class GameModel; diff --git a/src/client/Client.cpp b/src/client/Client.cpp index c5e29256c..886dfbbd7 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -803,10 +803,10 @@ Thumbnail * Client::GetPreview(int saveID, int saveDate) return new Thumbnail(saveID, saveDate, (pixel *)malloc((128*128) * PIXELSIZE), ui::Point(128, 128)); } -std::vector * Client::GetComments(int saveID, int start, int count) +std::vector * Client::GetComments(int saveID, int start, int count) { lastError = ""; - std::vector * commentArray = new std::vector(); + std::vector * commentArray = new std::vector(); std::stringstream urlStream; char * data; @@ -827,7 +827,7 @@ std::vector * Client::GetComments(int saveID, int start, int count) json::String tempUsername = commentsArray[j]["Username"]; json::String tempComment = commentsArray[j]["Text"]; commentArray->push_back( - new Comment( + new SaveComment( tempUserID.Value(), tempUsername.Value(), tempComment.Value() diff --git a/src/client/Client.h b/src/client/Client.h index ff571f85e..b00ea9312 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -63,7 +63,7 @@ public: LoginStatus Login(string username, string password, User & user); void ClearThumbnailRequests(); std::vector * SearchSaves(int start, int count, string query, string sort, string category, int & resultCount); - std::vector * GetComments(int saveID, int start, int count); + std::vector * GetComments(int saveID, int start, int count); Thumbnail * GetPreview(int saveID, int saveDate); Thumbnail * GetThumbnail(int saveID, int saveDate); Save * GetSave(int saveID, int saveDate); diff --git a/src/interface/Engine.h b/src/interface/Engine.h index f40f45fe5..d27826584 100644 --- a/src/interface/Engine.h +++ b/src/interface/Engine.h @@ -1,7 +1,6 @@ #pragma once #include -#include "SDL.h" #include "Singleton.h" #include "Platform.h" #include "Graphics.h" diff --git a/src/interface/Keys.h b/src/interface/Keys.h index 71c592815..85d861100 100644 --- a/src/interface/Keys.h +++ b/src/interface/Keys.h @@ -1,3 +1,5 @@ + +#if defined(USES_SDL) #define KEY_UP SDLK_UP #define KEY_DOWN SDLK_DOWN #define KEY_RIGHT SDLK_RIGHT @@ -22,3 +24,32 @@ #define BUTTON_LEFT SDL_BUTTON_LEFT #define BUTTON_MIDDLE SDL_BUTTON_MIDDLE #define BUTTON_RIGHT SDL_BUTTON_RIGHT + +#else + +#define KEY_UP 1 +#define KEY_DOWN 2 +#define KEY_RIGHT 3 +#define KEY_LEFT 4 +#define KEY_HOME 5 +#define KEY_END 6 +#define KEY_BACKSPACE 7 +#define KEY_DELETE 8 +#define KEY_TAB 9 +#define KEY_RETURN 10 +#define KEY_ENTER 11 +#define KEY_ESCAPE 12 + +#define KEY_CTRL 13 +#define KEY_ALT 14 +#define KEY_SHIFT 15 + +#define KEY_MOD_CONTROL 16 +#define KEY_MOD_ALT 17 +#define KEY_MOD_SHIFT 18 + +#define BUTTON_LEFT 19 +#define BUTTON_MIDDLE 20 +#define BUTTON_RIGHT 21 + +#endif diff --git a/src/interface/Platform.h b/src/interface/Platform.h index c57dca6ec..f9119f185 100644 --- a/src/interface/Platform.h +++ b/src/interface/Platform.h @@ -1,6 +1,6 @@ #pragma once - +typedef unsigned short Uint16; /* ***** Platform-ness ***** */ #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32_LEAN_AND_MEAN) diff --git a/src/powdertoyjava/OpenGLCanvasMacOS.h b/src/powdertoyjava/OpenGLCanvasMacOS.h new file mode 100644 index 000000000..3f3d68f63 --- /dev/null +++ b/src/powdertoyjava/OpenGLCanvasMacOS.h @@ -0,0 +1,33 @@ +#import + +#import +#import + +NSOpenGLPixelFormat* defaultPixelFormat(); + +NSOpenGLContext* ensureContext(NSOpenGLContext* openGLContext, NSView *view); + +typedef struct { + JAWT* awt; + JAWT_DrawingSurface* ds; + JAWT_DrawingSurfaceInfo* dsi; + JAWT_MacOSXDrawingSurfaceInfo* dsi_mac; + NSView *view; + NSOpenGLContext* openGLContext; +} ContextInfo; + +ContextInfo* getContext(JNIEnv *env, jobject canvas); + +void freeContext(JNIEnv *env, jobject canvas, ContextInfo* ci); + +#ifdef __cplusplus +extern "C" { +#endif + JNIEXPORT jboolean JNICALL Java_OpenGLCanvas_beginOpenGL(JNIEnv *env, jobject canvas); + JNIEXPORT void JNICALL Java_OpenGLCanvas_endOpenGL(JNIEnv *env, jobject canvas); + JNIEXPORT void JNICALL Java_OpenGLCanvas_updateOpenGL(JNIEnv *env, jobject canvas); + JNIEXPORT void JNICALL Java_OpenGLCanvas_allocOpenGL(JNIEnv *env, jobject canvas); + JNIEXPORT void JNICALL Java_OpenGLCanvas_releaseOpenGL(JNIEnv *env, jobject canvas); +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/src/powdertoyjava/OpenGLCanvasMacOS.mm b/src/powdertoyjava/OpenGLCanvasMacOS.mm new file mode 100644 index 000000000..009c32e21 --- /dev/null +++ b/src/powdertoyjava/OpenGLCanvasMacOS.mm @@ -0,0 +1,167 @@ +#include "OpenGLCanvasMacOS.h" + +static jfieldID ctxID = NULL; + +NSOpenGLPixelFormat* defaultPixelFormat() +{ + NSOpenGLPixelFormatAttribute attributes [] = { + NSOpenGLPFAWindow, + NSOpenGLPFADoubleBuffer, + NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16, + 0 + }; + return [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]; +} + +NSOpenGLContext* ensureContext(NSOpenGLContext* openGLContext, NSView *view) { + NSOpenGLContext* _openGLContext = openGLContext; + if (!_openGLContext) { + NSOpenGLPixelFormat* pixelFormat = defaultPixelFormat(); + _openGLContext = [[NSOpenGLContext alloc] + initWithFormat:pixelFormat + shareContext:nil]; + [pixelFormat release]; + } + if ([_openGLContext view] != view) { + [_openGLContext setView:view]; + } + [_openGLContext makeCurrentContext]; + + return _openGLContext; +} + +ContextInfo* getContext(JNIEnv *env, jobject canvas) +{ + if (!ctxID) { + jclass cls = env->GetObjectClass(canvas); + ctxID = env->GetFieldID(cls, "openGLContext", "J"); + } + + ContextInfo *ci = (ContextInfo *)(long)(env->GetLongField(canvas, ctxID)); + + if (!ci) { + ci = (ContextInfo *)calloc(sizeof(ContextInfo), 1); + ci->awt = (JAWT *)calloc(sizeof(JAWT), 1); + env->SetLongField(canvas, ctxID, (jlong)(long)ci); + } + + return ci; +} + +void freeContext(JNIEnv *env, jobject canvas, ContextInfo* ci) +{ + if (ci) { + free(ci->awt); + free(ci); + env->SetLongField(canvas, ctxID, 0L); + } +} + +JNIEXPORT jboolean JNICALL Java_OpenGLCanvas_beginOpenGL(JNIEnv *env, jobject canvas) +{ + ContextInfo *ci = getContext(env, canvas); + + // Lock the drawing surface + // You must lock EACH TIME before drawing + jint lock = ci->ds->Lock(ci->ds); + if (env->ExceptionOccurred()) { + env->ExceptionDescribe(); + } + assert((lock & JAWT_LOCK_ERROR) == 0); + + // Get the drawing surface info + ci->dsi = ci->ds->GetDrawingSurfaceInfo(ci->ds); + + // Check DrawingSurfaceInfo. This can be NULL on Mac OS X + // if the windowing system is not ready + if (ci->dsi != NULL) { + // Get the platform-specific drawing info + // We will use this to get at Cocoa and CoreGraphics + // See + ci->dsi_mac = (JAWT_MacOSXDrawingSurfaceInfo*)ci->dsi->platformInfo; + if (env->ExceptionOccurred()) { + env->ExceptionDescribe(); + } + + // Get the corresponding peer from the caller canvas + ci->view = ci->dsi_mac->cocoaViewRef; + ci->openGLContext = ensureContext(ci->openGLContext, ci->view); + + return JNI_TRUE; + } + + return JNI_FALSE; +} + +JNIEXPORT void JNICALL Java_OpenGLCanvas_endOpenGL(JNIEnv *env, jobject canvas) +{ + ContextInfo *ci = getContext(env, canvas); + + [ci->openGLContext flushBuffer]; + + // Free the DrawingSurfaceInfo + ci->ds->FreeDrawingSurfaceInfo(ci->dsi); + if (env->ExceptionOccurred()){ + env->ExceptionDescribe(); + } + + // Unlock the drawing surface + // You must unlock EACH TIME when done drawing + ci->ds->Unlock(ci->ds); + if (env->ExceptionOccurred()) { + env->ExceptionDescribe(); + } +} + +JNIEXPORT void JNICALL Java_OpenGLCanvas_updateOpenGL(JNIEnv *env, jobject canvas) +{ + ContextInfo *ci = getContext(env, canvas); + + [ci->openGLContext update]; +} + +JNIEXPORT void JNICALL Java_OpenGLCanvas_allocOpenGL(JNIEnv *env, jobject canvas) +{ + ContextInfo *ci = getContext(env, canvas); + + jboolean result = JNI_FALSE; + + // get the AWT + ci->awt->version = JAWT_VERSION_1_4; + result = JAWT_GetAWT(env, ci->awt); + if (env->ExceptionOccurred()) { + env->ExceptionDescribe(); + } + assert(result != JNI_FALSE); + + // Get the drawing surface. This can be safely cached. + // Anything below the DS (DSI, contexts, etc) + // can possibly change/go away and should not be cached. + ci->ds = ci->awt->GetDrawingSurface(env, canvas); + if (env->ExceptionOccurred()) { + env->ExceptionDescribe(); + } + assert(ci->ds != NULL); + + NSLog(@"Alloc Context %d", ci); +} + +JNIEXPORT void JNICALL Java_OpenGLCanvas_releaseOpenGL(JNIEnv *env, jobject canvas) +{ + ContextInfo *ci = getContext(env, canvas); + NSLog(@"Release Context %d", ci); + if (ci->openGLContext) { + if ([ci->openGLContext view] /* == self */) { + [ci->openGLContext clearDrawable]; + } + [ci->openGLContext release]; + } + + // Free the drawing surface (if not caching it) + ci->awt->FreeDrawingSurface(ci->ds); + if (env->ExceptionOccurred()) { + env->ExceptionDescribe(); + } + + freeContext(env, canvas, ci); +} \ No newline at end of file diff --git a/src/powdertoyjava/PowderToyJava.cpp b/src/powdertoyjava/PowderToyJava.cpp new file mode 100644 index 000000000..969d9b233 --- /dev/null +++ b/src/powdertoyjava/PowderToyJava.cpp @@ -0,0 +1,82 @@ +#if defined(USE_JNI) && defined(MACOSX) + +#include +#include +#include +#include + +#include "Config.h" +#include "PowderToyJava.h" +#include "Graphics.h" +#if defined(LIN32) || defined(LIN64) +#include "icon.h" +#endif + +#include "game/GameController.h" + +using namespace std; + +GameController * gameController; +ui::Engine * engine; + +int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0; +float fps = 0, delta = 1.0f; + +JNIEXPORT void JNICALL Java_PowderToy_initialise(JNIEnv * env, jobject canvas) +{ + //InitWindowMac(env, canvas); + + ui::Engine::Ref().g = new Graphics(); + + engine = &ui::Engine::Ref(); + engine->Begin(XRES+BARSIZE, YRES+MENUSIZE); + + gameController = new GameController(); + engine->ShowWindow(gameController->GetView()); + engine->SetFps(fps); +} + +JNIEXPORT void JNICALL Java_PowderToy_tick(JNIEnv * env, jobject canvas) +{ + engine->Tick(); +} + +JNIEXPORT void JNICALL Java_PowderToy_draw(JNIEnv * env, jobject canvas) +{ + engine->Draw(); + engine->g->Finalise(); +} + +JNIEXPORT void JNICALL Java_PowderToy_finish(JNIEnv * env, jobject canvas) +{ + ui::Engine::Ref().CloseWindow(); + delete gameController; + delete ui::Engine::Ref().g; +} + +JNIEXPORT jint JNICALL Java_PowderToy_getWidth(JNIEnv * env, jobject canvas) +{ + return XRES+BARSIZE; +} + +JNIEXPORT jint JNICALL Java_PowderToy_getHeight(JNIEnv * env, jobject canvas) +{ + return YRES+MENUSIZE; +} + +JNIEXPORT void JNICALL Java_PowderToy_mousePressed(JNIEnv * env, jobject canvas, jint mouseX, jint mouseY, jint mouseButton) +{ + engine->onMouseClick(mouseX, mouseY, mouseButton); +} + +JNIEXPORT void JNICALL Java_PowderToy_mouseReleased(JNIEnv * env, jobject canvas, jint mouseX, jint mouseY, jint mouseButton) +{ + engine->onMouseUnclick(mouseX, mouseY, mouseButton); +} + +JNIEXPORT void JNICALL Java_PowderToy_mouseMoved(JNIEnv * env, jobject canvas, jint mouseX, jint mouseY) +{ + engine->onMouseMove(mouseX, mouseY); +} + +#endif diff --git a/src/powdertoyjava/PowderToyJava.h b/src/powdertoyjava/PowderToyJava.h new file mode 100644 index 000000000..be0b8b1cc --- /dev/null +++ b/src/powdertoyjava/PowderToyJava.h @@ -0,0 +1,22 @@ +//#include +#include + +#ifndef POWDERTOYJAVA +#define POWDERTOYJAVA + +#ifdef __cplusplus +extern "C" { +#endif + JNIEXPORT void JNICALL Java_PowderToy_initialise(JNIEnv *, jobject); + JNIEXPORT void JNICALL Java_PowderToy_tick(JNIEnv *, jobject); + JNIEXPORT void JNICALL Java_PowderToy_draw(JNIEnv *, jobject); + JNIEXPORT void JNICALL Java_PowderToy_finish(JNIEnv *, jobject); + JNIEXPORT jint JNICALL Java_PowderToy_getWidth(JNIEnv * env, jobject canvas); + JNIEXPORT jint JNICALL Java_PowderToy_getHeight(JNIEnv * env, jobject canvas); + JNIEXPORT void JNICALL Java_PowderToy_mousePressed(JNIEnv * env, jobject canvas, jint mouseX, jint mouseY, jint mouseButton); + JNIEXPORT void JNICALL Java_PowderToy_mouseReleased(JNIEnv * env, jobject canvas, jint mouseX, jint mouseY, jint mouseButton); + JNIEXPORT void JNICALL Java_PowderToy_mouseMoved(JNIEnv * env, jobject canvas, jint mouseX, jint mouseY); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/src/preview/Comment.h b/src/preview/Comment.h index a53f6cd1f..c9a807c2f 100644 --- a/src/preview/Comment.h +++ b/src/preview/Comment.h @@ -8,13 +8,13 @@ #ifndef COMMENT_H_ #define COMMENT_H_ -class Comment +class SaveComment { public: int authorID; std::string authorName; std::string comment; - Comment(int userID, std::string username, std::string commentText): + SaveComment(int userID, std::string username, std::string commentText): authorID(userID), authorName(username), comment(commentText) { } diff --git a/src/preview/PreviewModel.cpp b/src/preview/PreviewModel.cpp index 71c4d4a0b..267571e4b 100644 --- a/src/preview/PreviewModel.cpp +++ b/src/preview/PreviewModel.cpp @@ -56,7 +56,7 @@ void * PreviewModel::updateSavePreviewT() void * PreviewModel::updateSaveCommentsT() { - std::vector * tempComments = Client::Ref().GetComments(tSaveID, 0, 10); + std::vector * tempComments = Client::Ref().GetComments(tSaveID, 0, 10); updateSaveCommentsFinished = true; return tempComments; } @@ -139,7 +139,7 @@ Save * PreviewModel::GetSave() return save; } -std::vector * PreviewModel::GetComments() +std::vector * PreviewModel::GetComments() { return saveComments; } diff --git a/src/preview/PreviewModel.h b/src/preview/PreviewModel.h index 91ca97218..26b6822cc 100644 --- a/src/preview/PreviewModel.h +++ b/src/preview/PreviewModel.h @@ -23,7 +23,7 @@ class PreviewModel { vector observers; Save * save; Thumbnail * savePreview; - std::vector * saveComments; + std::vector * saveComments; void notifyPreviewChanged(); void notifySaveChanged(); void notifySaveCommentsChanged(); @@ -53,7 +53,7 @@ public: PreviewModel(); Thumbnail * GetPreview(); Save * GetSave(); - std::vector * GetComments(); + std::vector * GetComments(); void AddObserver(PreviewView * observer); void UpdateSave(int saveID, int saveDate); void SetFavourite(bool favourite); diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp index dac53114c..d48a5a514 100644 --- a/src/preview/PreviewView.cpp +++ b/src/preview/PreviewView.cpp @@ -199,7 +199,7 @@ void PreviewView::NotifyCommentsChanged(PreviewModel * sender) int currentY = 0; ui::Label * tempUsername; ui::Textblock * tempComment; - std::vector * tempComments = sender->GetComments(); + std::vector * tempComments = sender->GetComments(); if(tempComments) { for(int i = 0; i < tempComments->size(); i++)