# ddx

Member

21

128 Neutral

• Rank
Member
1. ## Find prime numbers from 1 to n

Quote:Original post by Funkyjive Ok a number is prime if it is divisible ONLY by one and itself. So I guess I need to first loop through the numbers checking 1 through n if they are prime. You actually only need to go to floor(sqrt(n)). Any factor greater than sqrt(n) would have had a corresponding factor in the low end.
2. ## [C++] Problem passing a vector to a function

You can pass in the size by simply adding another parameter... void print_secret_word (vector <char> secret_word, str_siz length) { // whatever } but what is the point? You could just get the size inside print_secret_word() by calling secret_word.size(). But, going even further, why not just pass a string to print_secret_word() in the first place? void print_secret_word(const string& secret_word) { cout << secret_word; } const string word = "sheep"; print_secret_word(word);
3. ## plotting crossing lines

Here's how I'd do it. First, determine the distance between points, which you said yourself is trivial. Obtain the slope of the line between the points (y1 - y0) / (x1 - x0). Now, the slope of the new crossing lines is just the negative reciprocal of the slope you just calculated, since they are perpendicular. Now we are going to need the point on the original line where the new one crosses (let's say we're doing the one closest to (x1,y1)) Just make the difference of the points a vector, scale it to 1/10 length, and add to the first point. You now know the point on the original line. With m being the slope, the general equation for a line is y - y1 = m(x - x1) where the x1 and y1 is the point on the line just calculated. Repeat this for the rest of the crossing lines using an appropriate vector scale each time and you have all the lines. To calculate the end points of the line segments that go out 10 units, we again use the point on the original line and the slope of the crossing line. Multiply the slope by the distance 10. The slope is the change in y / change in x. So just add this to the point on the line to go in one direction, and subtract it from the point on the line to go in the other. That should do it.
4. ## pacman levels in opengl

Making the level graphics tile based should work. For that screenshot, you'd have a horizontal straight tile, a vertical straight one, one for each corner type, etc. So instead of rendering one big texture for the level background, you're rendering a grid of tiles. I've done this with a 2D racing game and it worked great.
5. ## Rotate a point around a user-defined axis

Create a quaternion from the axis and the angle you want to rotate by. Then multiply the point by the quaternion to get your transformed position.
6. ## Any way to force a console window?

Oops, sorry about the MaxConsoleLines. That field, according to MSDN, is: dwSize A COORD structure that contains the size of the console screen buffer, in character columns and rows. I have the Y set to 200. It can be whatever you want the buffer to be able to hold.
7. ## Best way to implement a logging feature

You might want to look into the "singleton pattern". I use it for the log manager in my game. Basically, it restricts a certain class to having at most one instantiation. Sometimes people overuse singletons, but for a LogManager, one instance is all you would ever want. So when I need to log a message, I just call LogManager::instance().log("blah") and it deals with the stream internally, and I can log messages from any part of my game that I need to without passing around a stream object.
8. ## Any way to force a console window?

Here's what I do in my LogManager class. Call FreeConsole() when you are done with it. All my output, even from Python and Lua, goes to the console window. void LogManagerConsole::redirectIo() { int hConHandle; long lStdHandle; CONSOLE_SCREEN_BUFFER_INFO coninfo; FILE *fp; // allocate a console for this app AllocConsole(); // set the screen buffer to be big enough to let us scroll text GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); coninfo.dwSize.Y = MaxConsoleLines; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); // position it const char* uniqueTitle = "my_console_window"; SetConsoleTitle(uniqueTitle); Sleep(40); HWND consoleWnd = FindWindow(NULL, uniqueTitle); SetWindowPos(consoleWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER); SetConsoleTitle("My log window"); // redirect unbuffered STDOUT to the console lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "w" ); *stdout = *fp; setvbuf( stdout, NULL, _IONBF, 0 ); // redirect unbuffered STDIN to the console lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "r" ); *stdin = *fp; setvbuf( stdin, NULL, _IONBF, 0 ); // redirect unbuffered STDERR to the console lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "w" ); *stderr = *fp; setvbuf( stderr, NULL, _IONBF, 0 ); // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well std::ios::sync_with_stdio(); }
9. ## If you're a Geek, you're a Virgin.

Most people are stupid. News at 11.
10. ## [Invalid]Decoding base64 strings

The first hit I got after typing "base64 C++" into Google gave me code to decode base64.
11. ## .png opacity/blending

I'm in the process of updating some old board games I made to OpenGL. I'm using .png files with transparency for all the textures, and it works great. Just remember to enable blending with glEnable(GL_BLEND).
12. ## C++ File I/O problem

In your code, you have: iter = vector.begin; iter != vector.end but begin and end and functions, so you should have: iter = vector.begin(); iter != vector.end();
13. ## Windows interface

You could look into a library like wxWidgets. It abstracts away the differences between platforms, so you don't need to know much of the Windows API, if anything. An even easier route is wxPython, which allows you to use wxWidgets from Python, but eliminates all the C++ boilerplate code of wxWidgets. Neither will give you the drag-n-drop you want to make your interface, but they are still very easy to get something going with. If you were doing this on OS X, I'd highly recommend Cocoa for ease of getting an interface going.
14. ## How to render vertices?

You don't always have to use vertex buffers for your vertices. You will want to most of the time, but sometimes a call to d3dDevice->DrawPrimitiveUP() can be useful. It lets you pass it a "user memory" pointer of vertex data, without having created a vertex buffer.
15. ## Can Python handle...

You may not want to have very low level things done in Python, like a renderer. But, you could certainly use Python for your game-specific code. Many libs have Python bindings. So, while you might be using Python to use the graphics lib, everything under the hood was done in C++.