Missing Mesh class on RenderScript support library - java

I'm following this sample in order to learn about RenderScript. I added the following at project.properties:
renderscript.target=19
renderscript.support.mode=true
sdk.buildtools=23.0.2
I wrote my .rs file, the ScriptC_myfilename was generated at gen folder but the android.support.v8.renderscript could not be resolved to a type, so I added the renderscript-v8.jar located at sdk/build-tools/android-4.4W/renderscript/lib as a library (configure Build Path >> Libraries >> add External JARs) and the problem was fixed.
After codding, I couldn't compile the code due to this error, found on ScriptC_filename.java at gen folder:
Mesh cannot be resolved to a Type
I search about the issue, trying to find the missing class and, I don't know, maybe manually implement this class as a part of my project, so Eclipse would be allowed to import it and fix the error, but I'm a bit confused since the Mesh class isn't cited even on android docs.
I also tried to import the renderscript-v8.jar at sdk\build-tools\android-4.4.2\renderscript\lib, as well as add import android.support.v8.renderscript.Mesh but with no success.
I don't know if this would help, but this is my ScriptC_Snow.java file (everything here was generated, I didn't edit it), The comments is the error showed on Eclipse
public class ScriptC_Snow extends ScriptC {
private static final String __rs_resource_name = "snow";
public ScriptC_Snow(RenderScript rs) {
this(rs,
rs.getApplicationContext().getResources(),
rs.getApplicationContext().getResources().getIdentifier(
__rs_resource_name, "raw",
rs.getApplicationContext().getPackageName()));
}
public ScriptC_Snow(RenderScript rs, Resources resources, int id) {
super(rs, resources, id);
__MESH = Element.MESH(rs); //the method MESH(RenderScript) is undefined for the type Element
__F32_2 = Element.F32_2(rs);
}
private Element __F32_2;
private Element __MESH;
private FieldPacker __rs_fp_F32_2;
private FieldPacker __rs_fp_MESH;
private final static int mExportVarIdx_snowMesh = 0;
private Mesh mExportVar_snowMesh; // Mesh cannot be resolved to a type
public synchronized void set_snowMesh(Mesh v) { // Mesh cannot be resolved to a type
setVar(mExportVarIdx_snowMesh, v);
mExportVar_snowMesh = v; // Mesh cannot be resolved to a type
}
public Mesh get_snowMesh() { // Mesh cannot be resolved to a type
return mExportVar_snowMesh; // Mesh cannot be resolved to a type
}
public Script.FieldID getFieldID_snowMesh() {
return createFieldID(mExportVarIdx_snowMesh, null);
}
private final static int mExportVarIdx_snow = 1;
private ScriptField_Snow mExportVar_snow;
public void bind_snow(ScriptField_Snow v) {
mExportVar_snow = v;
if (v == null) bindAllocation(null, mExportVarIdx_snow);
else bindAllocation(v.getAllocation(), mExportVarIdx_snow);
}
public ScriptField_Snow get_snow() {
return mExportVar_snow;
}
private final static int mExportVarIdx_wind = 2;
private Float2 mExportVar_wind;
public synchronized void set_wind(Float2 v) {
mExportVar_wind = v;
FieldPacker fp = new FieldPacker(8);
fp.addF32(v);
int []__dimArr = new int[1];
__dimArr[0] = 4;
setVar(mExportVarIdx_wind, fp, __F32_2, __dimArr);
}
public Float2 get_wind() {
return mExportVar_wind;
}
public Script.FieldID getFieldID_wind() {
return createFieldID(mExportVarIdx_wind, null);
}
private final static int mExportVarIdx_grav = 3;
private Float2 mExportVar_grav;
public synchronized void set_grav(Float2 v) {
mExportVar_grav = v;
FieldPacker fp = new FieldPacker(8);
fp.addF32(v);
int []__dimArr = new int[1];
__dimArr[0] = 4;
setVar(mExportVarIdx_grav, fp, __F32_2, __dimArr);
}
public Float2 get_grav() {
return mExportVar_grav;
}
public Script.FieldID getFieldID_grav() {
return createFieldID(mExportVarIdx_grav, null);
}
private final static int mExportFuncIdx_initSnow = 0;
public void invoke_initSnow() {
invoke(mExportFuncIdx_initSnow);
}
}
This is my renderscript code (.rs file):
#pragma version(1)
#pragma rs java_package_name(com.mypackage.script)
#include "rs_graphics.rsh"
rs_mesh snowMesh;
typedef struct __attribute__((packed, aligned(4))) Snow {
enter code here
float2 velocity;
float2 position;
uchar4 color;
enter code here
} Snow_t;
Snow_t *snow;
float2 wind;
float2 grav;
int root() {
rsgClearColor(0.0f, 0.0f, 0.0f, 0.0f);
rsgDrawMesh(snowMesh);
return 0;
}
void init() {
grav.x = 0;
grav.y = 18;
wind.x = rsRand(50)+20;
wind.y = rsRand(4) - 2;
}
void initSnow() {
enter code here
const float w = rsgGetWidth();
const float h = rsgGetHeight();
int snowCount = rsAllocationGetDimX(rsGetAllocation(snow));
Snow_t *pSnow = snow;
for (int i=0; i < snowCount; i++) {
pSnow->position.x = rsRand(w);
pSnow->position.y = rsRand(h);
pSnow->velocity.y = rsRand(60);
pSnow->velocity.x = rsRand(100);
pSnow->velocity.x -= 50;
uchar4 c = rsPackColorTo8888(255, 255, 255);
pSnow->color = c;
pSnow++;
}
}
I only used the SDK Manager to obtain the files, is there anything I'm missing? Anyone can give me a link to donwload the latest version of renderscript-v8.jar? Is there any link that I could use to see the missing class, in order to implement it in my project, so Eclipse'd be allowed to import and use it?
Thanks in advance.

You can't use the support library with rs_graphics.rsh. It doesn't support objects like rs_mesh, rs_font, or rs_program*. The support library is only for accessing RenderScript compute.

Related

Deep copy object that extends javafx Pane? [duplicate]

This question already has answers here:
Clone JavaFX Node?
(2 answers)
Closed 3 years ago.
I'm trying to deep copy an array of my custom object "Space", which extends javafx Pane.
I've tried using Kryo libraries, and implementing clonable. I could use work arounds that don't actually deep copy, but that will make my code 100x more janky.
Currently I have the class Space:
public class Space extends Pane {
private boolean available = true;
private boolean light;
private int x;
private int y;
private Peice peice;
private Label peiceImage;
private Rectangle border;
public Space() {}
public Space(int x, int y, boolean light) {
border = new Rectangle(80,80);
if (!light) {
border.setFill(Color.DARKGREEN);
border.setStroke(Color.DARKGREEN);
} else {
border.setFill(null);
border.setStroke(null);
}
peiceImage = new Label();
peiceImage.setTranslateX(16);
peiceImage.setTranslateY(16);
getChildren().addAll(border,peiceImage);
this.x = x;
this.y = y;
this.peice = null;
this.light = light;
}
//other unimportant methods.
}
I then try to copy in another file:
public Space[][] copyBoard(Space[][] thisBoard) {
Kryo kryo = new Kryo();
Space[][] copy = new Space[8][8];
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
Space thisSpace = thisBoard[x][y];
try {
kryo.setRegistrationRequired(false);
copy[x][y] = kryo.copy(thisSpace); //TODO This causes exception, dosent work
} catch (Exception e) {
System.out.println(e);
}
}
}
return copy;
}
Trying to use the kryo library I get this error:
com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): javafx.scene.layout.Region$3
But I'm not in any way commited to using kryo.
Thanks.
You can do deep copy using serialization and afterward deserialization. There are nice utils to do so on the web. For example SerializationUtils from Apache.
You can do something like the following:
Space clonedSpace = SerializationUtils.clone(space);
Docs

Better way to structure a java class with many variables

I have a java class called Player. The mechanics on how the player file interacts with the server would take a lot to explain and I have no issues with the file, I just want to improve it and better my programming skills. So my question is, how would I take the many variables I have, ints, strings, booleans and make the code faster and more efficient/cleaner. Here is an example so you can see what the file looks like
enter code here
public boolean usingGlory = false;
public int[] woodcut = new int [3];
public int wcTimer = 0;
public int[] mining = new int [3];
public int miningTimer = 0;
public boolean fishing = false;
public int fishtimer = 0;
public int fishXP;
public int fishies = 0;
public int fishreqt = 0;
public int fishitem = 0;
public int fishemote = 0;
public int fishies2 = 0;
public int fishreq2 = 0;
public boolean isfishing = false;
public boolean attemptingfish = false;
public int smeltType; //1 = bronze, 2 = iron, 3 = steel, 4 = gold, 5 = mith, 6 = addy, 7 = rune
public int smeltAmount;
public int smeltTimer = 0;
public boolean smeltInterface;
public boolean patchCleared;
public int[] farm = new int[2];
public boolean antiFirePot = false;
public boolean Lattack = false;
public boolean Lranged = false;
public boolean Lmagic = false;
public boolean Ldefense = false;
public boolean Lstrength = false;
public boolean Lspecial = false;
public boolean inPits = false;
public int pitsStatus = 0;
public boolean inFunPk = false;
public boolean InDung = false;
You can break down the data into several classes, each of which contains information on a specific area.
For example, here, you have a group of variables that are related to fishing - these can be grouped into e.g. a FishingComponent class. There's also a group of information on smelting that could be split into a SmeltingComponent class, and so forth. This may lead to making the code more readable by taking groups of closely related information and functionality into their own classes.
For reusability's sake, you may wish to use an entity component system. These are popular in games programming as they allow the makeup of objects to be customised more than just inheritance. If there is, for example, an NPC that may hold information on fishing but does not have stats, the fishing component may be added to the NPC entity, and all of the code relating to fishing may be re-used.
You also mention making the code faster - unless you have a specific performance problem, you are probably attempting to prematurely optimise. However, you may wish to look into pooling objects if they need to be garbage collected a lot, as in games written in garbage collected languages, garbage collection cycles can be one of the biggest cause of framedrops.
You might want to apply two common OOP techniques: encapsulation and composition. First of all, it's bad practice to have a bunch of public fields in your class. Instead, encapsulate them using getter and setter methods. Here's an example for the first two variables:
private boolean usingGlory = false;
public boolean isUsingGlory() {
return usingGlory;
}
public void setUsingGlory(boolean using) {
usingGlory = using;
}
private int[] woodcut = new int[3];
public int[] getWoodcut() {
return woodcut;
}
public void setWoodcut(int[] woodcut) {
this.woodcut = woodcut;
}
For more info, see Wikipedia.
The second technique is composition. You're right not to like having so many variables in your class. Instead of having all the variables be part of the Player class, group them into classes containing related items. You can even group methods, when appropriate! Here's an example that uses your fishing variables:
public class Player {
//...
private FishingState fishingState = new FishingState();
//Getter and setter
//...
}
public class FishingState {
private boolean fishing = false;
private int timer = 0;
private int XP;
private int fishies = 0;
private int reqt = 0;
private int item = 0;
private int emote = 0;
private int fishies2 = 0;
private int req2 = 0;
//Getters and setters
//A constructor and some methods:
public FishingState(int XP) {
this.XP = XP;
}
public void resetTimer() {
timer = 0;
}
}
Now, player.fishemote = 3 becomes player.getFishingState().setEmote(3). Longer, but clearer.

How can I make the Entities in a ParallaxBackground move vertically in AndEngine?

Title says it all, currently I'm making a game where my character moves vertically and I want the ParallaxBackground entities to do the same, how can I achieve this?
One way to do this is to extend the ParallaxEntity contained in the ParallaxBackground class and have that move vertically, here's some code showing how I've done that. It's basically swapping the changes in x-coordinates to y-coordinates. I've adapted it to the current version of AndEngine, the original version of the code that targeted an old version can be found here.
import org.andengine.engine.camera.Camera;
import org.andengine.entity.IEntity;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.opengl.util.GLState;
public class VerticalParallaxEntity extends ParallaxEntity
{
final float mParallaxFactor;
final IEntity mEntity;
public VerticalParallaxEntity(final float pParallaxFactor, final IEntity pEntity)
{
super(pParallaxFactor, pEntity);
this.mParallaxFactor = pParallaxFactor;
this.mEntity = pEntity;
}
public void onDraw(final GLState pGLState, final Camera pCamera, final float pParallaxValue)
{
pGLState.pushModelViewGLMatrix();
{
final float cameraHeight = pCamera.getHeight();
final float entityHeightScaled = this.mEntity.getHeight() * this.mEntity.getScaleY();
float baseOffset = (pParallaxValue * this.mParallaxFactor) % entityHeightScaled;
while(baseOffset > 0) {
baseOffset -= entityHeightScaled;
}
pGLState.translateModelViewGLMatrixf(0, baseOffset, 0);
float currentMaxY = baseOffset;
do
{
this.mEntity.onDraw(pGLState, pCamera);
pGLState.translateModelViewGLMatrixf(0, entityHeightScaled, 0);
currentMaxY += entityHeightScaled;
} while(currentMaxY < cameraHeight);
}
pGLState.popModelViewGLMatrix();
}
}

JNA GetIfTable error

I need to get the index of a Network Interface in Windows.
There is NetworkInterface.getIndex() method to do this, but it's only available starting Java 7.
The requirement from the Boss is that the app needs to run in Java 6.
After some searching, I found JNA lib to call Win API functions.
The function itself is GetIfTable (http://msdn.microsoft.com/en-us/library/windows/desktop/aa365943(v=vs.85).aspx)
However, I've not been able to successfully call this function using JNA.
The return value of GetIfTable is always 122 which means ERROR_INSUFFICIENT_BUFFER. Even if I keep adding buffer it still says insufficient buffer, until when the supplied value reach certain number, then "Invalid memory access" error shows up.
Here's what I already did:
public interface IpHlpAPI extends StdCallLibrary {
IpHlpAPI INSTANCE = (IpHlpAPI) Native.loadLibrary("IpHlpAPI", IpHlpAPI.class);
public static class MIB_IFTABLE extends Structure {
public int dwNumEntries;
public MIB_IFROW table[] = new IpHlpAPI.MIB_IFROW[1];
public MIB_IFTABLE() {}
public MIB_IFTABLE(int size) {
this.allocateMemory(size);
}
#Override
protected List getFieldOrder() {
return Arrays.asList(new String[]{"dwNumEntries", "table"});
}
}
public static class MIB_IFROW extends Structure {
public char wszName[] = new char[256];
public int dwIndex;
public int dwType;
public int dwMtu;
public int dwSpeed;
public int dwPhysAddrLen;
public byte bPhysAddr[] = new byte[8];
public int dwAdminStatus;
public int dwOperStatus;
public int dwLastChange;
public int dwInOctets;
public int dwInUcastPkts;
public int dwInNUcastPkts;
public int dwInDiscards;
public int dwInErrors;
public int dwInUnknownProtos;
public int dwOutOctets;
public int dwOutUcastPkts;
public int dwOutNUcastPkts;
public int dwOutDiscards;
public int dwOutErrors;
public int dwOutQLen;
public int dwDescrLen;
public byte bDescr[] = new byte[256];
#Override
protected List getFieldOrder() {
return Arrays.asList(new String[]{"wszName", "dwIndex", "dwType", "dwMtu",
"dwSpeed", "dwPhysAddrLen", "bPhysAddr", "dwAdminStatus", "dwOperStatus",
"dwLastChange", "dwInOctets", "dwInUcastPkts", "dwInNUcastPkts", "dwInDiscards",
"dwInErrors", "dwInUnknownProtos", "dwOutOctets", "dwOutUcastPkts",
"dwOutNUcastPkts", "dwOutDiscards", "dwOutErrors", "dwOutQLen", "dwDescrLen", "bDescr"});
}
}
int GetIfTable(MIB_IFTABLE pIfTable, IntByReference pdwSize, boolean bOrder);
}
public static void main(String[] args) {
IpHlpAPI ipHlpApi = IpHlpAPI.INSTANCE;
IpHlpAPI.MIB_IFTABLE ifTable = new IpHlpAPI.MIB_IFTABLE();
IntByReference psize = new IntByReference(ifTable.size());
int status = ipHlpApi.GetIfTable(ifTable, psize, false);
if (status == 122) {
// Calculate the required number of elements in the MIB_IFROW array
ifTable = new IpHlpAPI.MIB_IFTABLE((psize.getValue() - 4) / ifTable.table[0].size());
psize.setValue(ifTable.size());
status = ipHlpApi.GetIfTable(ifTable, psize, false);
System.out.println(status);
}
System.exit(0);
}
Any ideas why is this happening?
Your input structure isn't large enough. You need to reallocate the table field to be sufficiently large based on the value returned in the pdwSize parameter.
Add a constructor to MIB_IFTABLE that indicates the size of the table array and initializes it accordingly. That will automatically give you a bigger buffer.
MIB_IFTABLE ifTable = new MIB_IFTABLE();
IntByReference psize = new IntByReference(ifTable.size());
int status = ipHlpApi.GetIfTable(ifTable, psize, false);
if (status == 122) {
// Calculate the required number of elements in the MIB_IFROW array
ifTable = new MIB_IFTABLE((psize.getValue() - 4) / ifTable.table[0].size());
psize.setValue(ifTable.size());
status = ipHlpApi.GetIfTable(ifTable, psize, false);
// If status is still 122, then there's an issue with the MIB_IFROW definition
// ...
}
EDIT
Change your constructor:
public MIB_IFTABLE(int nentries) {
this.dwNumEntries = nentries;
this.table = (MIB_IFROW[])new MIB_IFROW().toArray(nentries);
}
You should probably initialize your other ctor to have a default value for dwNumEntries; as it is you'll be passing in a default value of zero, which will guarantee you get a return value of 122 on the first call. I'm assuming dwNumEntries refers to the size of the array.

Detect the location of AppData\LocalLow with JNA

I'm trying to detect the location of AppData\LocalLow work on Java with JNA on Windows 7. But the closest function available for the job is:
W32API.HRESULT SHGetFolderPath(W32API.HWND hwndOwner,int nFolder,W32API.HANDLE
hToken,W32API.DWORD dwFlags,char[] pszPath)
Here we have the Solution in C#
But in my case, JAVA + JNA, I'm wondering how I can use the LocalLow GUID with SHGetFolderPath only, or maybe I should look at the problem from a different angle (maybe JNI would be better here?)
If somebody can help on that, thanks
Cheers
EDIT:
Ok, now I added SHGetKnownFolderPath, but here, it keeps returning me strings like that "?f"
static interface Shell32 extends Library {
public static final int MAX_PATH = 260;
public static final String FOLDERID_APPDATALOW = "{A520A1A4-1780-4FF6-BD18-167343C5AF16}";
static Shell32 INSTANCE = (Shell32) Native.loadLibrary("shell32",
Shell32.class, OPTIONS);
public int SHGetKnownFolderPath(Guid.GUID rfid, int dwFlags, HANDLE hToken,
char[] pszPath);
}
public static void main(String[] args) {
char[] pszPath = new char[Shell32.MAX_PATH];
Guid.GUID localLowId = Ole32Util.getGUIDFromString(Shell32.FOLDERID_APPDATALOW);
int hResult = Shell32.INSTANCE.SHGetKnownFolderPath(localLowId, 0, null, pszPath);
if (hResult == 0) {
String path = new String(pszPath);
int len = path.indexOf('\0');
path = path.substring(0, len);
System.out.println(path);
} else {
System.err.println("Error: " + hResult);
}
}
You can extend Shell32 (or create your own similar class) to get access to the SHGetKnownFolderPath API:
W32API.HRESULT SHGetKnownFolderPath(
Guid.GUID rfid,
W32API.DWORD dwFlags,
W32API.HANDLE hToken,
char[] pszPath);
This Question is Somewhat old However I managed to Solve this issue.
allow me to explain why Kyro approach does not work quite well. This is due
SHGetKnownFolderPath uses a POINTER and not a String or A character array (that why show odd output) . therefore you need to use something like this:
public int SHGetKnownFolderPath(Guid.GUID rfid, int dwFlags, HANDLE hToken, PointerByReference pszPath);
a pointer by reference...
so on this case i attach a example I used:
static interface Shell32 extends StdCallLibrary {
public static final String FOLDERID_APPDATALOW = "{A520A1A4-1780-4FF6-BD18-167343C5AF16}";
static Shell32 INSTANCE = (Shell32) Native.loadLibrary("shell32",
Shell32.class, W32APIOptions.UNICODE_OPTIONS);
public int SHGetKnownFolderPath(Guid.GUID rfid, int dwFlags, HANDLE hToken,
PointerByReference pszPath);
}
public static void main(String[] args) {
Guid.GUID localLowId = Ole32Util.getGUIDFromString(Shell32.FOLDERID_APPDATALOW);
PointerByReference e= new PointerByReference();
int hResult = Shell32.INSTANCE.SHGetKnownFolderPath(localLowId, 0, null, e);
if (hResult == 0) {
char delim='\0';
char Array[]=e.getValue().getCharArray(0,255);
for (int i = 0; i < Array.length; i++) {
if(Array[i]==delim){
char temparr[]=new char[i];
System.arraycopy(Array,0,temparr,0,i);
Array=temparr;
break;
}
}
/*dont forget to release the Pointer*/
Ole32.INSTANCE.CoTaskMemFree(e.getValue());
System.out.println(Array);
} else {
System.err.println("Error: " + hResult);
}
}
private static interface Ole32 extends StdCallLibrary {
Ole32 INSTANCE = (Ole32) Native.loadLibrary(
"Ole32", Ole32.class, W32APIOptions.UNICODE_OPTIONS);
void CoTaskMemFree(Pointer pv);
}

Categories