in java, is there a way to store a "reference" to an object? As in, if I was creating a program which wrote a html file and I had <div>s represented by objects, could I add a variable or something to the div object class which pointed to the parent div (as opposed to BEING the parent div). Imagine it like an arraylist with only one value, it contains a reference to the value but it's not the actual value.
All objects in java are references I believe.
so if I had this class:
[code]
class Div {
Div parentDiv;
Div(Div pD)
{
parentDiv = pD;
}
Div()
{
}
}
[/code]
and called it using
[code]
class Main {
public static void main(String[] args)
{
Div div1 = new Div();
Div div2 = new Div(div1);
}
}
[/code]
parentDiv would point to div1 rather than being a separate Div with all the same properties?
If I understand correctly, yes. Even the word 'new' suggests that that would be the behaviour.
Hmm, well "new" doesn't really have anything to do with parentDiv
But I'll try it and see if it works
[QUOTE=aero1444;27261324]Hmm, well "new" doesn't really have anything to do with parentDiv
But I'll try it and see if it works[/QUOTE]
"new" is the C++ keyword for allocating memory on the heap. The value returned from new is a pointer to a block of memory, not an object. So Java adopted the same keyword, which suggests that it behaves the same way.
[QUOTE=yakahughes;27261603]"new" is the C++ keyword for allocating memory on the heap. The value returned from new is a pointer to a block of memory, not an object. So Java adopted the same keyword, which suggests that it behaves the same way.[/QUOTE]
Tested it with this:
[code]
public class Main {
public static void main(String[] args) {
Div div1 = new Div("ABCD");
Div div2 = new Div(div1);
div2.parentDiv.name = "WXYZ";
System.out.println(div1.name);
}
}
[/code]
[code]
public class Div {
String name;
Div parentDiv;
Div(Div pD)
{
parentDiv = pD;
}
Div(String n)
{
name = n;
}
}
[/code]
and WXYZ is printed, so it does work like you say. Thanks!
I have done lua for awhile and have moved onto c++, i find it challenging in some ways and easy in other ways, but i have come to a decision to make my OWN 2D Tile based game like Zelda, just as a start of point on learning C++, i have come to a fault that i can't fix or find a work around, now im asking for help, i can't find is it either the code or the compiler...
I Am on Windows 7 and Using MS C++ Express 2008 and DarkGDK which is used for making Games, my problem is that when ever i add/change something in the code nothing happens with compiled, no errors come up its just visually nothing has changed in the Game window, im totally confused and don't know what to do... here is the code...
im not including the Class' in the Code only the Main.cpp if you need the .h's just tell me and ill post them up too, if you want to download the files to fix the problem again just ask me to upload them, this isn't far into the code, i only started today, so im happy giving it out as there is nothing to it anyway, i just want my problem to be fixed.
[Code]
#include "darkGDK.h"
#include "Player.h"
#include "WorldV.h"
void DarkGDK (void)
{
dbSyncOn;
dbSyncRate(60);
dbDisableEscapeKey;
dbSetImageColorKey(255,0,255);
dbLoadImage("grassWF.bmp",1);
dbLoadImage("GGUI.bmp",2);
dbCreateAnimatedSprite(3,"PlayerSides.bmp",3,1,3);
dbCreateAnimatedSprite(4,"PlayerSides2.bmp",3,1,4) ;
dbCreateAnimatedSprite(5,"PlayerUP.bmp",3,1,5);
dbCreateAnimatedSprite(6,"PlayerDown.bmp",3,1,6);
dbSprite(1,0,0,1);
dbSprite(2,0,0,2);
dbSprite(3,5,50,3);
dbSprite(4,150,50,4);
dbSprite(5,100,50,5);
dbSprite(6,200,50,6);
Player Player;
while (LoopGDK())
{
Player::movement()
if(dbUpKey() == 1)
{
dbPlaySprite(5,1,3,100);
}
else
{
dbSetSpriteFrame(5,1);
}
if(dbEscapeKey() == 1)
{
break;
}
dbSync();
}
for( int i = 1; i < 20; i++ )
{
dbDeleteImage(i);
dbDeleteSprite(i);
}
return;
}
[/Code]
Then you will need a second array, as the tokens won't stay in the program after compilation & linkage.
Various solutions include a map or an array of pairs (or a pair of arrays :v:).
I'm having trouble getting SFML working correctly on VC++ 2010; I've linked everything correctly and installed proper .dll's, but if i create an image it throws an error saying "the stack around sf::Image is corrupted".
[img]http://www.1337upload.net/files/SS-2011-01-07_19.53.03.png[/img]
Everything else seems to work correctly however.
Are you sure you're linking with libs compiled with VC2010 or using the platform v90 toolset?
nope i'm using the libs directly from the sdk pack, i'll figure out how to compile them for VC2010 thanks
Is there a tutorial on making a parser/translator because every time I start it, I realise that I've fucked it up somehow and have to restart the whole thing (it's pretty annoying because I've restarted about 3 times already).
[QUOTE=Niteshifter;27273131]Is there a tutorial on making a parser/translator because every time I start it, I realise that I've fucked it up somehow and have to restart the whole thing (it's pretty annoying because I've restarted about 3 times already).[/QUOTE]
What do you mean by parser/translator they are two different things.
[QUOTE=mmavipc;27273250]What do you mean by parser/translator they are two different things.[/QUOTE]
I know. My attempts to make an assembler (which includes a parser and translator) have failed each time.
[QUOTE=Niteshifter;27273521]I know. My attempts to make an assembler (which includes a parser and translator) have failed each time.[/QUOTE]
Oh, I see what you're trying to do. It really depends on what language. simple asm is easy
[code]for each line:
divide line into array, delimetrer is a space
1st element in array is opcode, rest are args.[/code]
[QUOTE=mmavipc;27273740]Oh, I see what you're trying to do. It really depends on what language. simple asm is easy
[code]for each line:
divide line into array, delimetrer is a space
1st element in array is opcode, rest are args.[/code][/QUOTE]
I think the problem may be to do with my own code writing. The book I learned from is horribly written to the point where it's own reference code has segfaults in the pointers section, tells you close to nothing with arrays, file communication, strings, etc. Unfortunately, it's the only book that the only bookstore in my city has to offer, other than C++ for dummies (is this a good book to get?).
[QUOTE=Niteshifter;27274121]I think the problem may be to do with my own code writing. The book I learned from is horribly written to the point where it's own reference code has segfaults in the pointers section, tells you close to nothing with arrays, file communication, strings, etc. Unfortunately, it's the only book that the only bookstore in my city has to offer, other than C++ for dummies (is this a good book to get?).[/QUOTE]
C++ for dummies is pretty good, You do know that there are free online resources like cplusplus.com right?
[QUOTE=mmavipc;27274149]C++ for dummies is pretty good, You do know that there are free online resources like cplusplus.com right?[/QUOTE]
I prefer using a book though because my netbook is dead and the desktop fan sounds like I'm in an industrial server room.
I've worked with Lua up until now; I'm a relative newbie to java, and am having massive problems with user input (mainly key input).
There are no errors that my compiler is notifying me of in the code, but the key code doesn't seem to be affecting the rest of it.
[code]
public void paint(Graphics g)
{
if(lpv > 0)
{
sym = "+";
}
if(lpv == 0)
{
sym = " ";
}
if(lpv < 0)
{
sym = "-";
}
g.drawString("key = |"+sym+lpv+"| ("+i+")", 10, lpy);
}
public void processKeyEvent(KeyEvent e)
{
if(e.getID() == KeyEvent.KEY_PRESSED)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
lpy = lpy+1;
lpv = lpv+1;
}
}
}
[/code]
lpv, lpy, and i are ints and sym is a string.
[QUOTE=Niteshifter;27273521]I know. My attempts to make an assembler (which includes a parser and translator) have failed each time.[/QUOTE]
I would have a simple array of std::pairs (std::pair<opcode-name as lower-case string, number of expected arguments), then parse each line by splitting them with " \n", check if the opcode-name is in the array, use the position in the array as opcode (which means you have to order them appropriately! If there are 'holes' (i.e. unassigned opcodes) then you could just place an empty or invalid string there) and parse each argument, probably just as a number.
[QUOTE=ZeekyHBomb;27269233]Then you will need a second array, as the tokens won't stay in the program after compilation & linkage.
Various solutions include a map or an array of pairs (or a pair of arrays :v:).[/QUOTE]
if thats for me can you give me an example, as i said im rather new to C++ and it looks like C++ Arrays are different to Lua Arrays :S
No, that was directed to the guy who posted right before me.
I can't help you, because I don't know DarkGDK. Perhaps you are trying to execute the wrong binary; you could try deleting it and look if it gets re-created upon building the project. If it's still the same the only advice I could give you is look at the docs of DarkSDK to double-check that everything is correct.
Im trying to do the NeHe tutorial 6, texturing a box
I have done the tutorils before this fine however i am having trouble with this one
my code is (im using SFML instead of GLUT)
[cpp]#include <iostream>
#include <SFML/Graphics.hpp>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
void createOpenGL()
{
// Set color and depth clear value
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 0.f);
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
}
AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL;
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r");
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
int main()
{
sf::Clock Clock;
sf::WindowSettings Settings;
Settings.AntialiasingLevel = 4; // Request 2 levels of antialiasing
sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL",sf::Style::Resize | sf::Style::Close,Settings);
App.ShowMouseCursor(false);
float rQuad = 0.0f;
bool fullscreen = false;
AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture
memset(TextureImage,0,sizeof(void *)*1);
GLfloat xrot = 0.0f; // X Rotation ( NEW )
GLfloat yrot = 0.0f; // Y Rotation ( NEW )
GLfloat zrot = 0.0f; // Z Rotation ( NEW )
GLuint texture[1];
///////////OpenGL Stuff
createOpenGL();
if (TextureImage[0]=LoadBMP("facepunch.bmp"))
{
glGenTextures(1, &texture[0]); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
}////////window
while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
// Some code for stopping application on close or when escape is pressed...
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
if (Event.Type == sf::Event::Closed)
App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F1))
{
fullscreen=!fullscreen;
if(fullscreen==true)
App.Create(sf::VideoMode(800, 600, 32), "SFML OpenGL",sf::Style::Fullscreen,Settings);
else if(fullscreen==false)
App.Create(sf::VideoMode(800, 600, 32), "SFML OpenGL",sf::Style::Resize | sf::Style::Close,Settings);
createOpenGL();
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
rQuad++;
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down))
rQuad--;
}
App.SetActive();
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(1.5f,0.0f,-6.0f);
glRotatef(rQuad,1.0f,1.0f,1.0f);
glBindTexture(GL_TEXTURE_2D, texture[0]);
// glColor3f( 0.5f, 0.5f, 0.5f);
glBegin(GL_QUADS); // Draw A Quad
// glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
// glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
// glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
// glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back)
// glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
// glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd();
App.Display();
}
return 0;
}
[/cpp]
the problem is that it doesnt load the texture and just displays a white texture
For one, you're missing
[cpp]glEnable( GL_TEXTURE_2D );[/cpp]
[QUOTE=Overv;27281481]For one, you're missing
[cpp]glEnable( GL_TEXTURE_2D );[/cpp][/QUOTE]
Does that go inside createOpenGL()?
[editline]8th January 2011[/editline]
[img]http://img831.imageshack.us/img831/5994/untitlebd.png[/img]
:D
Turns out as i was using the old tutorial code for the box creation i hadent given it the texture positions :P
Anyone happen to know the best way of creating a progress bar for the loading of a model, me and ricky had the idea of just increasing a float every time something was done, but this seems rather hacky?
So are there any better methods of implementing it
[QUOTE=ZeekyHBomb;27276637]I would have a simple array of std::pairs (std::pair<opcode-name as lower-case string, number of expected arguments), then parse each line by splitting them with " \n", check if the opcode-name is in the array, use the position in the array as opcode (which means you have to order them appropriately! If there are 'holes' (i.e. unassigned opcodes) then you could just place an empty or invalid string there) and parse each argument, probably just as a number.[/QUOTE]
I think I'll try this. It looks to be more easier than using two separate arrays.
One a side-note, I couldn't find the C++ for dummies, but I did get Calculus for dummies :v: (I keep forgetting vector formulas as well as other things I need).
Sorry, you need to Log In to post a reply to this thread.