diff options
Diffstat (limited to 'src/powdertoyjava')
| -rw-r--r-- | src/powdertoyjava/OpenGLCanvasMacOS.h | 35 | ||||
| -rw-r--r-- | src/powdertoyjava/OpenGLCanvasMacOS.mm | 169 | ||||
| -rw-r--r-- | src/powdertoyjava/OpenGLCanvasWin32.cpp | 169 | ||||
| -rw-r--r-- | src/powdertoyjava/OpenGLCanvasWin32.h | 35 | ||||
| -rw-r--r-- | src/powdertoyjava/PowderToyJava.cpp | 77 | ||||
| -rw-r--r-- | src/powdertoyjava/PowderToyJava.h | 21 |
6 files changed, 506 insertions, 0 deletions
diff --git a/src/powdertoyjava/OpenGLCanvasMacOS.h b/src/powdertoyjava/OpenGLCanvasMacOS.h new file mode 100644 index 0000000..cee495a --- /dev/null +++ b/src/powdertoyjava/OpenGLCanvasMacOS.h @@ -0,0 +1,35 @@ +#ifdef USE_JNI +#import <jawt_md.h> + +#import <Cocoa/Cocoa.h> +#import <AppKit/NSOpenGL.h> + +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 +#endif
\ No newline at end of file diff --git a/src/powdertoyjava/OpenGLCanvasMacOS.mm b/src/powdertoyjava/OpenGLCanvasMacOS.mm new file mode 100644 index 0000000..c169c5f --- /dev/null +++ b/src/powdertoyjava/OpenGLCanvasMacOS.mm @@ -0,0 +1,169 @@ +#ifdef USE_JNI +#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 <JavaVM/jawt_md.h> + 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); +} +#endif
\ No newline at end of file diff --git a/src/powdertoyjava/OpenGLCanvasWin32.cpp b/src/powdertoyjava/OpenGLCanvasWin32.cpp new file mode 100644 index 0000000..7028d86 --- /dev/null +++ b/src/powdertoyjava/OpenGLCanvasWin32.cpp @@ -0,0 +1,169 @@ +#if defined(USE_JNI) && defined(WIN) +#include "OpenGLCanvasWin32.h" + +static jfieldID ctxID = NULL; + +int defaultPixelFormat(PIXELFORMATDESCRIPTOR* pfd) +{ + ::ZeroMemory( pfd, sizeof( PIXELFORMATDESCRIPTOR ) ); + pfd->nSize = sizeof( PIXELFORMATDESCRIPTOR ); + pfd->nVersion = 1; + pfd->dwFlags = PFD_DRAW_TO_WINDOW | + PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; + pfd->iPixelType = PFD_TYPE_RGBA; + pfd->cColorBits = 24; + pfd->cDepthBits = 16; + pfd->iLayerType = PFD_MAIN_PLANE; + return 0; +} + +HGLRC ensureContext(JAWT_Win32DrawingSurfaceInfo* dsi_win, HGLRC hRC) { + + if (!hRC) { + int iFormat; + PIXELFORMATDESCRIPTOR pfd; + defaultPixelFormat(&pfd); + + iFormat = ChoosePixelFormat( dsi_win->hdc, &pfd ); + SetPixelFormat( dsi_win->hdc, iFormat, &pfd ); + + hRC = wglCreateContext( dsi_win->hdc ); + } + if (1 && wglGetCurrentDC() != dsi_win->hdc) { + wglMakeCurrent( dsi_win->hdc, hRC ); + } + + return hRC; +} + +ContextInfo* getContext(JNIEnv *env, jobject canvas) +{ + ContextInfo *ci; + if (!ctxID) { + jclass cls = env->GetObjectClass(canvas); + ctxID = env->GetFieldID(cls, "openGLContext", "J"); + } + + 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) +{ + jint lock; + ContextInfo *ci = getContext(env, canvas); + + // Get the drawing surface. This can be safely cached -- not in win32 + // 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); + + // Lock the drawing surface + // You must lock EACH TIME before drawing + 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 <JavaVM/jawt_md.h> + ci->dsi_win = (JAWT_Win32DrawingSurfaceInfo*)ci->dsi->platformInfo; + if (env->ExceptionOccurred()) { + env->ExceptionDescribe(); + } + + // Get the corresponding peer from the caller canvas + ci->hRC = ensureContext(ci->dsi_win, ci->hRC); + + return JNI_TRUE; + } + + return JNI_FALSE; +} + +JNIEXPORT void JNICALL Java_OpenGLCanvas_endOpenGL(JNIEnv *env, jobject canvas) +{ + ContextInfo *ci = getContext(env, canvas); + + SwapBuffers( ci->dsi_win->hdc ); + + // 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(); + } + + // Free the drawing surface (if not caching it) + ci->awt->FreeDrawingSurface(ci->ds); + if (env->ExceptionOccurred()) { + env->ExceptionDescribe(); + } +} + +JNIEXPORT void JNICALL Java_OpenGLCanvas_updateOpenGL(JNIEnv *env, jobject canvas) +{ + ContextInfo *ci = getContext(env, canvas); + + wglMakeCurrent( ci->dsi_win->hdc, ci->hRC ); +} + +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); +} + +JNIEXPORT void JNICALL Java_OpenGLCanvas_releaseOpenGL(JNIEnv *env, jobject canvas) +{ + ContextInfo *ci = getContext(env, canvas); + if (ci->hRC) { + wglDeleteContext(ci->hRC); + } + + freeContext(env, canvas, ci); +} +#endif diff --git a/src/powdertoyjava/OpenGLCanvasWin32.h b/src/powdertoyjava/OpenGLCanvasWin32.h new file mode 100644 index 0000000..bff6b20 --- /dev/null +++ b/src/powdertoyjava/OpenGLCanvasWin32.h @@ -0,0 +1,35 @@ +#ifdef USE_JNI +#import <jawt_md.h> + +#include <windows.h> +#include <assert.h> +#include <gl/gl.h> + +int defaultPixelFormat(PIXELFORMATDESCRIPTOR* pfd); + +HGLRC ensureContext(JAWT_Win32DrawingSurfaceInfo* dsi_win, HGLRC hRC); + +typedef struct { + JAWT* awt; + JAWT_DrawingSurface* ds; + JAWT_DrawingSurfaceInfo* dsi; + JAWT_Win32DrawingSurfaceInfo* dsi_win; + HGLRC hRC; +} 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 +#endif diff --git a/src/powdertoyjava/PowderToyJava.cpp b/src/powdertoyjava/PowderToyJava.cpp new file mode 100644 index 0000000..835fdc4 --- /dev/null +++ b/src/powdertoyjava/PowderToyJava.cpp @@ -0,0 +1,77 @@ +#if defined(USE_JNI) + +#include <time.h> +#include <iostream> +#include <sstream> +#include <string> + +#include "Config.h" +#include "PowderToyJava.h" +#include "graphics/Graphics.h" + +#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) +{ + 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 0000000..d39a8a9 --- /dev/null +++ b/src/powdertoyjava/PowderToyJava.h @@ -0,0 +1,21 @@ +#include <jni.h> + +#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 |
