• What do you need help with? V. 3.0
    4,884 replies, posted
How can i use the console for input in XNA while the game is running? If i put Console.ReadLine() somewhere in the code, it will freeze the whole game. Halp?
[QUOTE=Funley;33763866]How can i use the console for input in XNA while the game is running? If i put Console.ReadLine() somewhere in the code, it will freeze the whole game. Halp?[/QUOTE] [url=http://msdn.microsoft.com/en-us/library/system.console.keyavailable.aspx]Console.KeyAvailable[/url] for polling if there's input in the buffer, [url=http://msdn.microsoft.com/en-us/library/471w8d85.aspx]Console.ReadKey[/url] for reading it. However, this is only one key/character at a time, so there's probably a better method somewhere.
Hey there folks, got a little problem here. I'm using Microsoft Visual C++ 2010 Express, and while my very first program works without debugging, it just won't function in other cases. All I get are these strange messages: "Cannot find or open the PDB file" I'm working with a book for newbs like me, and checked the code about 4 times already - still no chance to get this stuff to use. It should open this little cmd-window and show "Hello world!" and wait for input to finally close the window, but well, it just starts and clothes right after. The end of the message finally tells (Sorry, it's german) Das Programm "[5260] Listing_1.exe: Systemeigen" wurde mit Code 0 (0x0) beendet. The Program "[5620] Listing_1.exe: native" was closed with Code 0 (0x0). So... where's the goddamn error? It's pretty frustrating and I don't want to use the old 2008 one, even though it's considered to work better with that book. Here's the full code: [code] // C++ für Spieleprogrammierer // Listing 1.1 // Es wird ein Begrüßungstext ausgegeben // #include <iostream> using namespace std; // Hauptprogramm // int main () { cout << "Hello World!\n"; return 0; } [/code]
It's working perfectly. The PDB file missing messages is just the debugger trying to find debug information for all the loaded .dll files, you can pretty much ignore them. Starting and closing right after is because you're not doing anything in the main function that would block it from closing. When the function returns, the program has completed and it closes. The return code 0 ("closed with Code 0") is EXIT_SUCCESS, ie. the program ran successfully. I would suggest running your program from the command line, like it was supposed to be ran. This way it functions as intended, since the window doesn't close right after the program finishes. However, if you want to debug, add this before the return: [cpp]std::cin.get();[/cpp] This function waits for input from the user, effectively pausing the program until you press a key.
Wow! Thanks, now it works - shows no message about "Press anything, bla bla bla", but it works! Awesome! :) Saved that line in a seperate textfile and will add some information on that to make sure I understand it in the future. Thank you a thousand times - I will now continue learning this little magical language... :D
How do I get right deltatime? I've used this: [code] float deltatime = 0, deltastart = 0, deltaend = 0; deltastart = clock(); deltatime = (deltastart - deltaend); deltaend = clock(); X+= deltatime/5; [/code] The problem is that it makes moving look laggy and may cause other various problems.
[QUOTE=rute;33765743]How do I get right deltatime? I've used this: [code] float deltatime = 0, deltastart = 0, deltaend = 0; deltastart = clock(); deltatime = (deltastart - deltaend); deltaend = clock(); X+= deltatime/5; [/code] The problem is that it makes moving look laggy and may cause other various problems.[/QUOTE] It's an issue with precision. clock() is actually a timer which returns a (possibly large) [i]integer[/i] value, so you're sure to end up with significant rounding error by casting to float before taking the difference. While integers have 32 bits of precision, floats only have ~24, the rest of the bits in a 32-bit floating-point value are used to provide additional [i]dynamic range[/i] (which is not the same as precision). Try this and see if it helps: [code] float deltatime = 0 unsigned int deltastart = 0, deltaend = 0; deltastart = clock(); /* change in scale here -- deltatime gives time in _seconds_, adjust elsewhere accordingly */ deltatime = (float)(deltastart - deltaend)/(float)CLOCKS_PER_SECOND; deltaend = clock(); X+= deltatime*200; [/code]
Im trying to setup openGL using glmaths but my ortho projection isnt working. so these are the matricies im using: [code] float width = App.GetWidth(); float height = App.GetHeight(); orthoMatrix = glm::ortho<float>(0.0f, width, height, 0.0f, 0.0f, 1.0f); perspectiveMatrix = glm::perspective<float>(65, (width / height) ,0.1f,100.0f); cameraMatrix = glm::lookAt(glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3(0.0f), glm::vec3( 0, 1, 0 ));[/code] then: [code] MVP = (*perspectiveMatrix) * (*cameraMatrix) * modelMatrix; ////////////////////////////////// void main(){ uv = UV; gl_Position = MVP * Position; } [/code] using the perspective matrix it works fine, but using the ortho one it doesnt draw
[QUOTE=Richy19;33766427]using the perspective matrix it works fine, but using the ortho one it doesnt draw[/QUOTE] Your orthographic projection area is like a big thin sheet... probably not what you want... Visualize a box. You're looking down from the top of this box, anything contained within scaled to fit the screen. Anything outside the box will not be drawn. This is orthographic projection. Your box is [i]width x height x 1.0[/i], which is likely to be something like [i]1920 x 1080 x 1[/i], i.e. it's a sheet of paper with almost no depth. This might be OK if you're doing something in 2.5D, but it's not going to work for your typical 3D scene. If you're doing 3D, try something like: [code] float ratio = width / height; glm::ortho<float>(-250.0f * ratio, 250.0f * ratio, -250.0f, 250.0f, -250.0f, 250.0f); [/code]
Can somebody help a horrible new C++ programmer out? I have been trying to get FMOD working so I can get sound in my program. It can play the sound fine about 15 times, then it stops working. [cpp] void ljud (char* soundtype) { FMOD_SYSTEM *soundsystem; FMOD_CHANNEL *channel = 0; FMOD_System_Create(&soundsystem); FMOD_System_Init(soundsystem, 32, FMOD_INIT_NORMAL, NULL); FMOD_SOUND *s1, *nope; FMOD_System_CreateSound(soundsystem, "ljud/click.wav", FMOD_HARDWARE, 0, &s1); FMOD_System_CreateSound(soundsystem, "ljud/nope.wav", FMOD_HARDWARE, 0, &nope); if (soundtype == "fy") { FMOD_System_PlaySound(soundsystem, FMOD_CHANNEL_FREE, s1, 0, &channel); } else if (soundtype == "nope") { FMOD_System_PlaySound(soundsystem, FMOD_CHANNEL_FREE, nope, 0, &channel); } else { cout << "Invalid soundtype, go fix the code you lazy bum\n"; } } [/cpp] I call the sound using [b]ljud("fy");[/b] or [b]ljud("nope");[/b] I have been trying to fix this for hours, can't do anything about it, I have tried moving the initializing of FMOD somewhere else but that just gives me a "not in scope" error. [editline]heckno[/editline] It seems that this is caused by FMOD initializing more than 15 times, how would I make it work without reinitializing it? [editline]dangit[/editline] If I only initialize it once, it crashes when playing the sound a second time. [editline]insert something witty here[/editline] If I use a [b]FMOD_System_Release(soundsystem);[/b] at the end of the function it works, but then I must have a sleep before it or the sound will not play and that just slows everything down. [editline]asdf[/editline] Nevermind, I'll stop being a lazy bum and restructure the code to make a decent main loop instead.
How would I go about making the string hold a space defined by gets()? [cpp]#include <iostream> #include <cstring> #include <cstdio> using namespace std; int main() { char str[80]; char *start, *end; int len; char t; cout << "Initial string: "; gets(str); cout << "\n"; len = strlen(str); start = str; end = &str[len-1]; while(start < end) { t = *start; *start = *end; *end = t; start++; end--; } cout << "\nReversed: " << str << "\n"; return 0; } [/cpp]
I'd avoid using gets. You've got a buffer overflow vulnerability there. Unless it's like an assignment or something where you've got no choice in the matter. If you're asking how to allocate space for an arbitrarily large string whose size isn't known in advance, there isn't a simple answer. What you basically have to do is read the string in chunks of a predetermined size and place those into a growable array. This is not provided by the C standard library, so you've either got to use something like [url=http://developer.gnome.org/glib/2.30/glib-Arrays.html]glib[/url] or write your own. It's not [url=https://github.com/ml32/datastruct/blob/master/array.c]terribly difficult[/url], basically you just check whether you have enough space to add the data and, if you don't, scale geometrically (double, multiply by 3/2, whatever) until it is large enough, realloc() the buffer to the new size, and copy the data. If it sounds like a pain in the ass, that's because it is. Doing anything with strings in C is painful. If you have the option, you might consider just allocating a large buffer in static memory and using that, throwing an error if you have to read anything longer than that buffer. Once you've read the string in, you can check its length with strlen() and malloc() an appropriate amount for long-term use. This approach would be like: [cpp] char* get_string() { static buf[1024]; scanf("%1023s", buf); buf[1023] = '\0'; int n = strlen(buf); char *str = (char*)malloc(n); memcpy(str, buf, n); return str; } [/cpp]
C++ [editline]17th December 2011[/editline] I know making an array using array[] and then assigning it to a string make the array of the size of that string (+1), so I thought that could be applicable here. I mean [cpp]char str[] = "bob" // str now holds 4 elements [/cpp] [editline]17th December 2011[/editline] I'm a beginner by the way. That was code from a book, and I'm trying to improve on it
That's like a bastardized mix of C and C++ :\ I'm really not a C++ guy, but I think the STL includes a string class built-in that's compatible with iostreams? Couldn't you do something like "std::string str; std::cin >> str;"?
[QUOTE=ROBO_DONUT;33769604]That's like a bastardized mix of C and C++ :\ I'm really not a C++ guy, but I think the STL includes a string class built-in that's compatible with iostreams? Couldn't you do something like "std::string str; std::cin >> str;"?[/QUOTE] It's completely C++. I've not learned that section yet, I was just wondering if there was a simple answer.
[QUOTE=Meatpuppet;33769756]It's completely C++.[/QUOTE] gets() is an old C function, one which even C programmers avoid. Anytime you include a header that starts with a 'c', like '[b]c[/b]stdio' it's usually including C headers, in this case 'stdio.h'. Again, I write more C than I write C++, but I don't think using a lot of C routines is considered idiomatic C++. They're more provided for compatibility purposes.
[QUOTE=ROBO_DONUT;33769845]gets() is an old C function, one which even C programmers avoid. Anytime you include a header that starts with a 'c', like '[b]c[/b]stdio' it's usually including C headers, in this case 'stdio.h'. Again, I write more C than I write C++, but I don't think using a lot of C routines is considered idiomatic. They're more provided for compatibility purposes.[/QUOTE]I thought you meant that the code I posted was a bastardized mix that doesn't work with either. I've been taught to use gets() when I need someone to enter a string with spaces, as cin >> doesn't do that.
[QUOTE=Meatpuppet;33769865] I've been taught to use gets() when I need someone to enter a string with spaces, as cin >> doesn't do that.[/QUOTE] What
[QUOTE=esalaka;33769908]What[/QUOTE] If you need the user to type a string, and you use cin, then when you try to output the string again, it just outputs everything before the first space. [cpp]#include <iostream> using namespace std; int main() { char str[80]; cout << "enter a string: "; cin >> str; cout << "you entered: " << str; // only outputs first word return 0; }[/cpp]
You could cin.get(sb) where sb is a std::stringbuf, then get the string by doing sb.str()
[QUOTE=Meatpuppet;33769865]I thought you meant that the code I posted was a bastardized mix that doesn't work with either.[/QUOTE] No, it's definitely valid C++, but I just don't think it's idiomatic. [QUOTE=Meatpuppet;33769865]I've been taught to use gets() when I need someone to enter a string with spaces, as cin >> doesn't do that.[/QUOTE] Ah. I didn't know that. Still, you're better off avoiding gets(). It doesn't allow you to provide a size limit and is perfectly content to continue on past the end of the buffer and corrupt the shit out of the stack. Worse yet, it's a security hazard because nine-times-out-of-ten the function return pointer isn't too far past the end of that buffer and a skilled hacker can easily use that unconditional return jump to skip to his own malicious code. Instead, I'd use scanf() [i]with a specified size limit[/i] for the string and read the string in chunks. Since you're using C++, you don't need to write a growable array implementation. std::string will allow you to append the partial strings and handle all the memory allocation junk for you.
For the record I didn't actually know this before I went to cplusplus.com again. That site taught me everything about the standard library.
I'll use that when I know what that means; thanks though.
Fuck shit dicks automerge [editline]WHAT[/editline] HOW
ninjas [editline]17th December 2011[/editline] FUCK
#include <thread/mutex.h> :v:
[QUOTE=ROBO_DONUT;33769956]No, it's definitely valid C++, but I just don't think it's idiomatic.[/QUOTE] I made an example with idiomatic C++. [cpp]#include <string> #include <iostream> int main(int argc, char* argv[]) { std::string input; std::cout << "Enter a string: " << std::flush; std::getline(std::cin, input); std::cout << "You entered: " << input << std::endl; std::string reversed(input.rbegin(), input.rend()); std::cout << "Reversed: " << reversed << std::endl; return 0; }[/cpp] std::cin << only pulls one "input object" from the buffer, which is why you only got up to the first space. std::getline can be used for the whole inputted line. The reversing part is pretty simple. The std::string operator takes a start and end iterator as an argument, which we get from the input string. The string::rbegin function returns a reversed beginning iterator, rend returns a reversed ending iterator. So we just give those two to the constructor and it creates a reversed string. A bit simpler than the C method. e: For the record, I didn't know about the std::cin behavior either, so I just googled it quickly :v:
So I planned on creating an evolution simulation over the weekend. There were going to be 2 major kinds of objects. Particles & Organisms... Particles were the simplest, so I started with them first. I had predetermined that all my collisions were going to be radial. I wanted to do an early stress test for a practical environment with 300 particles. This is what happened FPS = 1 to 2 ... and in good moments, 3 [IMG]http://i.imgur.com/aLqcy.png[/IMG] As you can see, I'm running it in gamemaker. It was going to be a small weekend project, but with this FPS disaster, it no longer can be. It MUST be something wrong with gamemaker... not my code. How fast can you guys simulate this situation? I'm suppose to be dumping ~100 more objects that have line+circle collision checking and about 3x more radial collision checks. BTW.... these aren't GM objects colliding with other GM objects. this is an array of variables checking for collision X[300] Y[300] r[300] I figured recoding the collision system would grant me more speed. This is the code I'm running. Thought I was being slick when I was dodging redundant collision checks with "for(j=i;j<0;j+=1)" ... but apparently, it's still incredibly slow [code] for(i=-(chemicals.particles);i<0;i+=1) { for(j=i;j<0;j+=1) { if i!=j { r=0 if (i<0) { ikey=(-1*i)-1 Y1=chemicals.Y[ikey]; X1=chemicals.X[ikey]; r+=chemicals.r[ikey]; } if (j<0) { jkey=(-1*j)-1 X2=chemicals.X[jkey]; Y2=chemicals.Y[jkey]; r+=chemicals.r[jkey]; } if point_distance(X1,Y1,X2,Y2)<=r { chemicals.xSpeed[ikey]*=-1 chemicals.ySpeed[ikey]*=-1 chemicals.xSpeed[jkey]*=-1 chemicals.ySpeed[jkey]*=-1 } } } } [/code] [editline]18th December 2011[/editline] that last block is so that i can see a collision has happened. I wanted to make sure that my reduncancy avoidance still worked. it did... but it's still slow
Well I dunno if anyone can help but I'll ask anyways. I'm doing some 3D stuff in XNA (because OpenGL and DirectX are a bit above me). Anyways I have the camera bound to a 3D object a certain distance away. I want the camera to have all the same rotations as the object. Basically I have it all working except when it rotates on it's Z-axis, which is where I draw a blank. So basically what I have to do is determine the camera's "up" vector in order to do this. So far I have: [code]upVector.X = -1 * (float)(Math.Cos(boundObj.getRotationX() + (Math.PI / 2)) * Math.Sin(boundObj.getRotationY())); upVector.Y = (float)(Math.Sin(boundObj.getRotationX() + (Math.PI / 2))); upVector.Z = -1 * (float)(Math.Cos(boundObj.getRotationY()) * Math.Cos(boundObj.getRotationX() + (Math.PI / 2)));[/code] Which works really well and all (it's actually what I used to find the objects "forward" vector just with 90 degrees more rotation in the Y). But I don't know how to incorporate the Z-Rotation. XNA can figure that out given the camera's up vector but again I'm not sure how to change it based off the Z rotation as well (as that code ^ only adjusts it based off the X and Y rotations, it should adjust if for all 3). Picture because I'm probably bad at explaining it: [img]http://dl.dropbox.com/u/11517902/rotations.png[/img]
I'm trying to make it so that when I press a mouse button it places the currently selected tile at that location and so far I have [code]MouseState mouse = Mouse.GetState(); if (mouse.LeftButton == ButtonState.Pressed) tileFilled = true; TileX = (int)Math.Floor((float)(mouse.X / tileWidth)); TileY = (int)Math.Floor((float)(mouse.Y / tileHeight)); SquarePosition.X = (TileX * tileWidth)+(tileWidth/2); SquarePosition.Y = (TileY * tileHeight) + (tileHeight / 2); if (tileFilled == true) spriteBatch.Draw at SquarePosition;[/code] I don't know what I'm supposed to be doing at the bottom to make it actually draw my sprite.
Sorry, you need to Log In to post a reply to this thread.