[img]http://filesmelt.com/dl/seethissuckerrighthere.png[/img]
See that little sucker there, in the red box? How do I add one of those to my application(s)?
Not much, added input. I'll re-write the quad as an entity and throw a server together.
[img]http://localhostr.com/files/c7490b/Fate.png[/img]
Some help with Java right now would be appreciated
Alright, I'm trying to have a method read the parameters of an object of a custom class. So basically I have a method that has to take an object as a parameter. How to I get that method to access the parameters (integers) of that object?
First, notation: [b]parameters[/b] are what you pass to methods, [b]instance variables[/b] are the values associated with individual objects.
You need to write "get" methods for your custom class that return the needed instance variables e.g.
[code]
public Foo getBar() {
return bar;
}
[/code]
then you would call object.getBar(); in the method.
Or you could do it the horrible way and make the instance variables public.
-snip- figured it out. Thanks for the help Larikang.
You should be able to directly access quad's members. In Java, private just means private to the class, not private to the instance. So try quad.constA, quad.constB, etc.
edit: lol late
[QUOTE=shill le 2nd;25563191]You should be able to directly access quad's members. In Java, private just means private to the class, not private to the instance. So try quad.constA, quad.constB, etc.
edit: lol late[/QUOTE]
If it makes you feel better that what I did lol.
Anyways I got another question now. How would I go about having my program print out nothing in place of one of my constants when it equals 0.
Here's my code.
[code]
private void print()
{
String str1 = "X^2 + ";
String str2 = "X + ";
if(constA == 0)
{
str = "";
}
if(constB == 0)
{
str2 = "";
}
if(constC == 0)
{
}
System.out.print(constA + str1 + constB + str2 + constC);
}
[/code]
Had to change StringBuilder formula; to StringBuilder formula = negw StringBuilder(); but besides that it worked like a charm. Thanks
I have no doubt I am missing a very obvious way but I am trying to create the start of a game with OpenGL, basically what I am trying to do is move everything round the centre character. I am doing it this way as I am having multiple camera angles following the main character. So far I have the main character at 0,0,0 (is 3D) and a few blocks scattered around to test it. testpos goes up when the up key is pressed and down when the down key is pressed, spin increases with right and decreases with left.
When the main character is moved forwards I want all other objects to be transformed -testpos backwards. When the main character is rotated I want every object to rotated -spin in the opposite direction around the origin. Sounds pretty simple but I can't get them to work together, the code I have so far is:
[code]
gluLookAt(0,30,-40,0,0,0,0,0,1); //Look at origin where main guy is
glPushMatrix();
drawGuy();//the actual process of drawing him is handles elsewhere but is just a texture drawn over the origin
glPopMatrix();
glRotatef(-spin,0,1,0);//Rotate after transformation to make it seem like the guy is turning
glTranslatef(0,0,testpos); //Transform across the z value
glPushMatrix();//All other objects are contained in here
glPushMatrix();
glTranslatef(5,0,10); //Draw a cube and transform it
drawCube();
glPopMatrix();
glPushMatrix();
glTranslate(10,0,5);
drawCube();
glPopMatrix();
glPopMatrix();
[/code]
At the moment the two cubes are rotating round the origin (as I want) but moving in the direction they are facing rather than only along the Z axis.
Translate before rotating.
[QUOTE=ZeekyHBomb;25571231]Translate before rotating.[/QUOTE]
Then they rotate around the axis where they were originally generated before been transformed.
[code]translate
push
rotate
translate
draw
translate to next offset
draw
pop[/code]
I think that works.
Ok, been trying to organise alot of my code into their own objects and stuff. I am now stuck at this error:
FIXED
This my game.h:
[code]
//Game.h
//Defines the various variables and functions that the game class will contain.
#ifndef GAME_H
#define GAME_H
#include "player.h"
#include "tile.h"
class Game
{
private:
Player *m_Player;
Tile *m_Tiles [ 2 ];
sf::RenderWindow* m_Window;
public:
Game( sf::RenderWindow& app );
void Render();
void Update();
void HandleEvents();
sf::RenderWindow GetWindow( );
};
#endif
[/code]
This is my game.cpp:
[code]
//Game.cpp
//Implements the functions that the game object has...
#include "stdafx.h"
#include "game.h"
#include "player.h"
/*Game::Game()
{
//m_Window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "The Game");
}*/
Game::Game( sf::RenderWindow& app )
{
m_Window = &app;
m_Player = new Player( "C:/sprite.tga" );
}
void Game::HandleEvents()
{
sf::Event Event;
while(m_Window->GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
{
m_Window->Close();
}
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
{
m_Window->Close();
}
}
//Realtime inputs
if(m_Window->GetInput().IsKeyDown(sf::Key::Left))
{
m_Player->Move( -100, 0 );
}
else if(m_Window->GetInput().IsKeyDown(sf::Key::Right))
{
m_Player->Move( 100, 0 );
}
else if(m_Window->GetInput().IsKeyDown(sf::Key::Up))
{
if( !m_Player->IsJumping() )
{
m_Player->Jump( -500 );
}
}
else if(m_Window->GetInput().IsKeyDown(sf::Key::Down))
{
m_Player->Move( 100, 0 );
}
}
void Game::Render()
{
//m_Window->Clear();
//m_Window->Draw( m_Player->GetSprite() );
m_Player->Draw( (*m_Window) );
}
void Game::Update()
{
HandleEvents();
}
sf::RenderWindow Game::GetWindow()
{
return (*m_Window);
}
[/code]
If there's any errors here, please let me know. Me and Overv spend like 2 hours trying to get this to work.. ( Thanks Overv, even though you hate me now. <'33 )
However now that I try to draw the stuff into the window, it's all just a white screen...
I initialise my Game object with this:
[code]
sf::RenderWindow App( sf::VideoMode( 800, 600, 32 ), "The Game" );
Game Game( App );
[/code]
And this is my Gameloop:
[code]
while( App.IsOpened() )
{
Game.Render();
Game.Update();
}
[/code]
Player.draw function looks like this:
[code]
void Player::Draw( sf::RenderWindow &Target )
{
Target.Draw( m_Sprite );
Target.Draw( m_World );
}
[/code]
What am I doing wrong?
Nevermind i got it working
[QUOTE=Capsup;25576878]Ok, been trying to organise alot of my code into their own objects and stuff. I am now stuck at this error:
What am I doing wrong?[/QUOTE]
window->Display()
lol
I'm making a c program that can take input either as a string provided from the command line or from a text file.
I wanted to code it so that once I initialized everything, I could use the same logic for both types of input rather than coding it two different ways. Basically, I want a pointer pointing to the file data as if it were a char array.
Here's what I tried:
[cpp]
FILE *ifp;
char *ptr;
ifp = fopen("input.txt", "r");
ptr = (char *)ifp;
[/cpp]
But then ptr starts reading gibberish, I guess because ifp is pointing at a FILE struct rather than the actual file data. Is there a way to do what I want, or do I have to read in the data first using fgetc or fread?
You definitely have to fread to a buffer. What you are trying to do makes no sense.
[QUOTE=Larikang;25579568]I'm making a c program that can take input either as a string provided from the command line or from a text file.
I wanted to code it so that once I initialized everything, I could use the same logic for both types of input rather than coding it two different ways. Basically, I want a pointer pointing to the file data as if it were a char array.
Here's what I tried:
-code snip-
But then ptr starts reading gibberish, I guess because ifp is pointing at a FILE struct rather than the actual file data. Is there a way to do what I want, or do I have to read in the data first using fgetc or fread?[/QUOTE]
stdin is a file handle too:
[cpp]
#include <stdio.h>
void readStuff(FILE* f)
{
/* read from f */
}
int main()
{
readStuff(fopen("input.txt", "r"));
readStuff(stdin);
return 0;
}[/cpp]
(left out error handling to keep it short)
Note that both stdin and file handles acquired with fopen are only handles, no file content exists in memory until you call one of the read or scan functions.
edit:
Oh, did you mean a command line argument? :frown:
[QUOTE=Larikang;25579568]
But then ptr starts reading gibberish, I guess because ifp is pointing at a FILE struct rather than the actual file data. Is there a way to do what I want, or do I have to read in the data first using fgetc or fread?[/QUOTE]
so if you want to take the entire content out of the file for reading a c function to do this looks like this
[code]
char *AllocateReadFile(const char *filename)
{
// locals
FILE *file;
char *chars_out;
long size;
//try to open file
file = fopen( filename, "r" );
if( file != 0 )
{
// file handle opened no problems.
// seek to the end of the file to get its size
fseek( file, 0L, SEEK_END );
// get the position of the stream ( which would be the size )
size = ftell( file );
// move the position of the stream back to the start
fseek( file, 0L, SEEK_SET );
// allocate the char buffer
chars_out = (char *) malloc( size );
// read the data in
fread( chars_out, 1, size, file );
// close the file.
fclose(file); file = 0;
}else{
chars_out = 0;
}
return chars_out;
}[/code]
That copies the entire content of a file to a char * allocated with malloc. If the file couldnt be opened it would return NULL. Important though you call free after your done using any data returned there, otherwise you'll leak memory. Also you'll most likely need to be careful about what kind of txt file your using/encoding. --don't forget to free whatever comes out of that function that is non null-- ie char *data = AllocateReadFile("input.txt"); .. do things here ..; free(data);
if theres random characters at the end of the string try changing that malloc call in the function to something like this:
chars_out = (char *) memset( malloc( size + 1 ), 0, size + 1 );
which will fill the buffer with zeros and put a extra character at the end to ensure the strings null terminated
so anyways you should be able to treat stuff from command line and contence of a file the same through char buffers. you'll just need to free the data afterwards. you can use something like [url=http://www.cplusplus.com/reference/clibrary/cstring/strtok/]strtok[/url] (c compatible) to seperate commands out of the string if your using a main function that has a char*argv[], int argc style. Though it would probably be easier to just use strtok and use the main function that does not give you a array of char *'s ( char*[] ) handling everything the same.
I'm trying to write a generic factory-class using C++0x variadic templates.
It seems that using a variadic template in a function-pointer typedef doesn't work well.
[cpp]#include <map>
#include <memory>
#include <string>
template<typename T, typename ...Args>
class GenericFactory
{
public:
typedef T*(*CtorType)(Args...);
typedef std::map<std::string, CtorType> CtorMap;
public:
void registerSubclass(const std::string &name, CtorType newfunc);
std::unique_ptr<T> createInstance(const std::string &name, Args ...arguments) const;
protected:
typename CtorMap::const_iterator getConstructor(const std::string &name) const{ return ctors.find(name); }
bool isValidEntry(const typename CtorMap::const_iterator iter) const{ return iter != ctors.end(); }
private:
CtorMap ctors;
};
#include <cassert>
template<typename T, typename ...Args>
void GenericFactory<T, Args>::registerSubclass(const std::string &name, CtorType newfunc) //error: ‘CtorType’ has not been declared //error: invalid use of incomplete type ‘class GenericFactory<T, Args>’
{
#if defined(DEBUG)
const typename CtorMap::const_iterator iter(std::lower_bound(ctors.begin(), ctors.end(), name));
assert(!isValidEntry(iter) || iter->first != name);
ctors.insert(std::make_pair(name, newfunc), iter);
#else
ctors.insert(std::make_pair(name, newfunc));
#endif
}
template<typename T, typename ...Args>
std::unique_ptr<T> GenericFactory<T, Args>::createInstance(const std::string &name, Args ...arguments) const //error: invalid use of incomplete type ‘class GenericFactory<T, Args>’
{
const typename CtorMap::const_iterator ctor(getConstructor(name));
assert(isValidEntry(ctor));
return std::unique_ptr<T>(ctor->second(arguments...));
}[/cpp]
That's with g++ 4.4.3.
clang errors and finally crashes trying to use the stdlibc++ (4.4.3) for (all/most/some?) C++0x and TR1 stuff.
[editline]23rd October 2010[/editline]
Just tried typedef'ing a std::function<T*(Args...)> instead, same errors.
[editline]23rd October 2010[/editline]
Solution:
[cpp]void GenericFactory<T, Args...>::regsiterSubclass(...)[/cpp]
[QUOTE=VeryNiceGuy;25555557][img_thumb]http://filesmelt.com/dl/seethissuckerrighthere.png[/img_thumb]
See that little sucker there, in the red box? How do I add one of those to my application(s)?[/QUOTE]
Anyone?
make your window style re-sizable?
nevermind, i had to change SizeGripStyle to 'Show'.
[cpp]package itpat;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Translator {
public static void translate(String text) {
Scanner sc = null;
try {
sc = new Scanner(new File("compile.txt"));
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "Could not load compile.bfc", "Brainf**k Converter", -1);
System.exit(0);
}
String myArray[][] = new String[37][2];
int i = 0;
String temp;
String temp2[] = new String[3];
while (sc.hasNext()){
temp = sc.nextLine();
temp2 = temp.split("<>");
myArray[i][0] = temp2[0];
myArray[i][1] = temp2[1];
i++;
}
String temp3 = text;
char temp4;
String output = "";
for(int j = 0;j < temp3.length(); j++) {
for(int k=0; k<i; k++){
if(temp3.charAt(j) == myArray[k][0].charAt(0)) {
output = output + myArray[k][1];
}
}
}
JOptionPane.showMessageDialog(null, output, "DOES IT WORK?", -1);
}
}
[/cpp]
I am having problem with that and keep getting this error
[code]run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at itpat.Translator.translate(Translator.java:25)
at itpat.Main.translate(Main.java:122)
at itpat.Main.actionPerformed(Main.java:113)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 12 seconds)[/code]
All help appreciated. thanks
[code]Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at itpat.Translator.translate(Translator.java:25)[/code]
[code]25: myArray[i][1] = temp2[1];[/code]
myArray: [code]String myArray[][] = new String[37][2];[/code]
temp2: [code]temp2 = temp.split("<>");[/code]
guess what :v:
[QUOTE=CountNoobula;25613434][cpp]
while (sc.hasNext()){
temp = sc.nextLine();
temp2 = temp.split("<>");
myArray[i][0] = temp2[0];
myArray[i][1] = temp2[1];
i++;
}
[/cpp]
I've only dealt with java a couple of times, But since its a array index out of bounds exception. I'm betting that temp.split call is sometimes returning a string[] with less than 2 elements.
I'm pretty sure the new String[3] for temp2 initializer is pretty much avoided. ( I don't think temp.split will fill it, but just assign a new string[] array to it. )
So try checking temp2.length before reading the [0] and [1] values.
You probably increment i more than 37 times. What file are you loading?
-ignore it, zeeky is right-
Can someone help me? I am trying to get the console to wait for a key press and then display what key the user input. my code (mind you i am extremely new to this) Oh and this is C#!
[Csharp]
/*
* Created by SharpDevelop.
* User: Brandon
* Date: 10/24/2010
* Time: 11:46 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace nothing
{
class Program
{
public static void Main(string[] args)
{
string Letter;
Console.WriteLine("Press any key");
Console.ReadKey();
Letter = Keyboard.GetState();
Console.Clear();
Console.WriteLine("You have pressed {0}",Letter);
}
}
}
[/Csharp]
I think I need to add a "using" thing (i forget what they are called) for input
Sorry, you need to Log In to post a reply to this thread.