Skip to content

Commit

Permalink
Changed Shader class to no longer pass Java objects to native code.
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Dinklage <pdinklag@googlemail.com>
  • Loading branch information
pdinklag committed Mar 16, 2013
1 parent e7d1ac5 commit 02f88e0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 54 deletions.
30 changes: 15 additions & 15 deletions include/JSFML/JNI/org_jsfml_graphics_Shader.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 18 additions & 19 deletions src/cpp/JNI/org_jsfml_graphics_Shader.cpp
@@ -1,8 +1,7 @@
#include <JSFML/JNI/org_jsfml_graphics_Shader.h>

#include <JSFML/Intercom/JavaEnum.hpp>
#include <JSFML/Intercom/Intercom.hpp>
#include <JSFML/Intercom/NativeObject.hpp>
#include <JSFML/Intercom/Transform.hpp>

#include <SFML/Graphics/Shader.hpp>

Expand Down Expand Up @@ -45,16 +44,16 @@ JNIEXPORT jlong JNICALL Java_org_jsfml_graphics_Shader_nativeCreate (JNIEnv *env
/*
* Class: org_jsfml_graphics_Shader
* Method: nativeLoadFromSource1
* Signature: (Ljava/lang/String;Lorg/jsfml/graphics/Shader/Type;)Z
* Signature: (Ljava/lang/String;I)Z
*/
JNIEXPORT jboolean JNICALL Java_org_jsfml_graphics_Shader_nativeLoadFromSource1
(JNIEnv *env, jobject obj, jstring source, jobject type) {
(JNIEnv *env, jobject obj, jstring source, jint type) {

const char *utf8 = env->GetStringUTFChars(source, NULL);

jboolean result = THIS(sf::Shader)->loadFromMemory(
std::string(utf8),
(sf::Shader::Type)JavaEnum::ordinal(env, type));
(sf::Shader::Type)type);

env->ReleaseStringUTFChars(source, utf8);
return result;
Expand Down Expand Up @@ -82,10 +81,10 @@ JNIEXPORT jboolean JNICALL Java_org_jsfml_graphics_Shader_nativeLoadFromSource2

/*
* Class: org_jsfml_graphics_Shader
* Method: nativeSetParameter
* Method: nativeSetParameterFloat
* Signature: (Ljava/lang/String;F)V
*/
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameter__Ljava_lang_String_2F
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameterFloat
(JNIEnv *env, jobject obj, jstring name, jfloat x) {

const char *utf8 = env->GetStringUTFChars(name, NULL);
Expand All @@ -95,10 +94,10 @@ JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameter__Ljava_

/*
* Class: org_jsfml_graphics_Shader
* Method: nativeSetParameter
* Method: nativeSetParameterVec2
* Signature: (Ljava/lang/String;FF)V
*/
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameter__Ljava_lang_String_2FF
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameterVec2
(JNIEnv *env, jobject obj, jstring name, jfloat x, jfloat y) {

const char *utf8 = env->GetStringUTFChars(name, NULL);
Expand All @@ -108,10 +107,10 @@ JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameter__Ljava_

/*
* Class: org_jsfml_graphics_Shader
* Method: nativeSetParameter
* Method: nativeSetParameterVec3
* Signature: (Ljava/lang/String;FFF)V
*/
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameter__Ljava_lang_String_2FFF
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameterVec3
(JNIEnv *env, jobject obj, jstring name, jfloat x, jfloat y, jfloat z) {

const char *utf8 = env->GetStringUTFChars(name, NULL);
Expand All @@ -121,10 +120,10 @@ JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameter__Ljava_

/*
* Class: org_jsfml_graphics_Shader
* Method: nativeSetParameter
* Method: nativeSetParameterVec4
* Signature: (Ljava/lang/String;FFFF)V
*/
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameter__Ljava_lang_String_2FFFF
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameterVec4
(JNIEnv *env, jobject obj, jstring name, jfloat x, jfloat y, jfloat z, jfloat w) {

const char *utf8 = env->GetStringUTFChars(name, NULL);
Expand All @@ -134,26 +133,26 @@ JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameter__Ljava_

/*
* Class: org_jsfml_graphics_Shader
* Method: nativeSetParameter
* Signature: (Ljava/lang/String;Lorg/jsfml/graphics/Transform;)V
* Method: nativeSetParameterMat4
* Signature: (Ljava/lang/String;Ljava/nio/Buffer;)V
*/
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameter__Ljava_lang_String_2Lorg_jsfml_graphics_Transform_2
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameterMat4
(JNIEnv *env, jobject obj, jstring name, jobject xform) {

const char *utf8 = env->GetStringUTFChars(name, NULL);
THIS(sf::Shader)->setParameter(
std::string(utf8),
JSFML::Transform::ToSFML(env, xform));
JSFML::Intercom::decodeTransform(env, xform));

env->ReleaseStringUTFChars(name, utf8);
}

/*
* Class: org_jsfml_graphics_Shader
* Method: nativeSetParameter
* Method: nativeSetParameterSampler2d
* Signature: (Ljava/lang/String;Lorg/jsfml/graphics/Texture;)V
*/
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameter__Ljava_lang_String_2Lorg_jsfml_graphics_Texture_2
JNIEXPORT void JNICALL Java_org_jsfml_graphics_Shader_nativeSetParameterSampler2d
(JNIEnv *env, jobject obj, jstring name, jobject texture) {

const char *utf8 = env->GetStringUTFChars(name, NULL);
Expand Down
40 changes: 20 additions & 20 deletions src/java/org/jsfml/graphics/Shader.java
@@ -1,14 +1,12 @@
package org.jsfml.graphics;

import org.jsfml.internal.SFMLErrorCapture;
import org.jsfml.internal.SFMLNative;
import org.jsfml.internal.SFMLNativeObject;
import org.jsfml.internal.StreamUtil;
import org.jsfml.internal.*;
import org.jsfml.system.Vector2f;
import org.jsfml.system.Vector3f;

import java.io.IOException;
import java.io.InputStream;
import java.nio.Buffer;
import java.nio.file.Path;
import java.util.Objects;

Expand Down Expand Up @@ -45,7 +43,8 @@ private CurrentTextureType() {
* <p/>
* This is required only if you wish to use JSFML shaders in custom OpenGL code.
*
* @param shader the shader to activate.
* @param shader the shader to activate, or {@code null} to indicate that no shader
* is to be used.
*/
public static native void bind(ConstShader shader);

Expand Down Expand Up @@ -94,7 +93,7 @@ protected void nativeSetExPtr() {
@SuppressWarnings("deprecation")
protected native void nativeDelete();

private native boolean nativeLoadFromSource1(String source, Type shaderType);
private native boolean nativeLoadFromSource1(String source, int shaderType);

private native boolean nativeLoadFromSource2(String vertSource, String fragSource);

Expand All @@ -119,8 +118,7 @@ public void loadFromSource(String source, Type shaderType)

SFMLErrorCapture.start();
final boolean result = nativeLoadFromSource1(
Objects.requireNonNull(source),
Objects.requireNonNull(shaderType));
Objects.requireNonNull(source), shaderType.ordinal());

final String msg = SFMLErrorCapture.finish();

Expand Down Expand Up @@ -214,7 +212,7 @@ public void loadFromFile(Path vertexShaderFile, Path fragmentShaderFile)
new String(StreamUtil.readFile(fragmentShaderFile)));
}

private native void nativeSetParameter(String name, float x);
private native void nativeSetParameterFloat(String name, float x);

/**
* Sets a float parameter ({@code float}) value in the shader.
Expand All @@ -223,10 +221,10 @@ public void loadFromFile(Path vertexShaderFile, Path fragmentShaderFile)
* @param value the parameter's value.
*/
public void setParameter(String name, float value) {
nativeSetParameter(Objects.requireNonNull(name), value);
nativeSetParameterFloat(Objects.requireNonNull(name), value);
}

private native void nativeSetParameter(String name, float x, float y);
private native void nativeSetParameterVec2(String name, float x, float y);

/**
* Sets a 2-component-float ({@code vec2}) parameter value in the shader.
Expand All @@ -236,7 +234,7 @@ public void setParameter(String name, float value) {
* @param y the parameter's value.
*/
public void setParameter(String name, float x, float y) {
nativeSetParameter(Objects.requireNonNull(name), x, y);
nativeSetParameterVec2(Objects.requireNonNull(name), x, y);
}

/**
Expand All @@ -249,7 +247,7 @@ public void setParameter(String name, Vector2f v) {
setParameter(name, v.x, v.y);
}

private native void nativeSetParameter(String name, float x, float y, float z);
private native void nativeSetParameterVec3(String name, float x, float y, float z);

/**
* Sets a 3-component-float ({@code vec3}) parameter value in the shader.
Expand All @@ -260,7 +258,7 @@ public void setParameter(String name, Vector2f v) {
* @param z the parameter's value.
*/
public void setParameter(String name, float x, float y, float z) {
nativeSetParameter(Objects.requireNonNull(name), x, y, z);
nativeSetParameterVec3(Objects.requireNonNull(name), x, y, z);
}

/**
Expand All @@ -273,7 +271,7 @@ public void setParameter(String name, Vector3f v) {
setParameter(name, v.x, v.y, v.z);
}

private native void nativeSetParameter(String name, float x, float y, float z, float w);
private native void nativeSetParameterVec4(String name, float x, float y, float z, float w);

/**
* Sets a 4-component-float ({@code vec4}) parameter value in the shader.
Expand All @@ -285,7 +283,7 @@ public void setParameter(String name, Vector3f v) {
* @param w the parameter's value.
*/
public void setParameter(String name, float x, float y, float z, float w) {
nativeSetParameter(Objects.requireNonNull(name), x, y, z, w);
nativeSetParameterVec4(Objects.requireNonNull(name), x, y, z, w);
}

/**
Expand All @@ -302,7 +300,7 @@ public void setParameter(String name, Color color) {
(float) color.a / 255.0f);
}

private native void nativeSetParameter(String name, Transform xform);
private native void nativeSetParameterMat4(String name, Buffer xform);

/**
* Sets a matrix (mat4) parameter value in the shader.
Expand All @@ -311,10 +309,12 @@ public void setParameter(String name, Color color) {
* @param xform the parameter's value.
*/
public void setParameter(String name, Transform xform) {
nativeSetParameter(Objects.requireNonNull(name), Objects.requireNonNull(xform));
nativeSetParameterMat4(
Objects.requireNonNull(name),
IntercomHelper.encodeTransform(xform));
}

private native void nativeSetParameter(String name, Texture texture);
private native void nativeSetParameterSampler2d(String name, Texture texture);

/**
* Sets a texture (sampler2D) parameter value in the shader.
Expand All @@ -323,7 +323,7 @@ public void setParameter(String name, Transform xform) {
* @param texture the parameter's value.
*/
public void setParameter(String name, ConstTexture texture) {
nativeSetParameter(Objects.requireNonNull(name), (Texture) Objects.requireNonNull(texture));
nativeSetParameterSampler2d(Objects.requireNonNull(name), (Texture) Objects.requireNonNull(texture));
}

private native void nativeSetParameterCurrentTexture(String name);
Expand Down

0 comments on commit 02f88e0

Please sign in to comment.