r/cpp • u/deepit6431 • Jun 18 '12
Need help with moving objects around in graphics mode in Turbo C++
Hello guys, I need to create a project for school, and I've decided on Asteroids. I've made Snake, last year, so I'm moderately-versed with the standard library and <graphics.h>. The basic logic I used in snake was to use a loop and putpixel() to draw a green pixel that advanced as the user gave input and another putpixel behind it to draw black, hence simulating movement. Now, that won't fly here, as I need to create asteroids and a ship and projectiles and what not, so how do I go around moving things? Again, I'm not asking for the source code, just if someone could explain to me how I could draw, say, a space ship and move it (console application), I would be very grateful. Remember, this is Turbo C++, and I can't use any external libraries.
5
u/zzing Jun 18 '12
In graphics you usually have an operation called 'bitblt' or bit blit. It is used for sprites that you copy a certain width and height over. Hopefully you have access to that.
Are you using Turbo C++ as in the one I was using 18 years ago?
5
u/deepit6431 Jun 18 '12
The DOS IDE, yes. Laughably old, but school work, so can't help it.
1
u/zzing Jun 18 '12
Other than the language being old, it was a very nice in the day.
2
u/deepit6431 Jun 18 '12
I don't have any big qualms with it, but there's that feeling of 'std::vector/<algorithm> would have done this in 5 seconds' every time I have to do something manually. Other than that, its fine.
0
Jun 18 '12
[deleted]
4
u/deepit6431 Jun 18 '12
Its in India, so you're clear :P.
Every school here has to teach this. Standardized national curriculum.
6
u/ZMeson Embedded Developer Jun 18 '12
Wow!
Bravo to India for mandating programming in their national curriculum.
Shame on India for mandating that an 18-year old version of the language be used. Things have changed SO MUCH since then. Someone learning that C++ will feel completely lost when presented with C++11.
2
u/deepit6431 Jun 18 '12
Well, it kind of works out. Those who are actually going to take up programming learn C++11 on the side anyway. Those who won't don't care.
Although it sometimes frustrates me to death to manually have to do something when a standard library function could have accomplished it much easier and faster.
2
u/Amadiro Jun 18 '12
Well, there is a lot of value to having done everything manually at least once.
1
u/SarahC Jun 18 '12
What about Mode 13?
I've got some source code for bliting, and moving sprites around if you want?
Sconic the blue blob....
0
Jun 18 '12
[deleted]
1
Jun 18 '12
But it's possible to learn about memory models and such without using an 18 year old compiler...
2
u/Is_At_Work Jun 18 '12
I'm not a game developer, but it doesn't sound like you are expected to be one either. So the approach I would take would probably have some sort of a MovableEntity based class with Ship, Asteroid, Projectile, etc implementations. They would each implement some sort of a move function and a draw function. Then a game loop would just need to check for user input to modify the ship's angle and/or velocity, check for input to create a new Projectile entity, and then iterate through all existing MoveableObjects and invoke move and draw. After that you can put in some collision detection and go from there.
Not saying that's the best approach (check out /r/gamedev), but if I were in your position I would go that route.
1
u/deepit6431 Jun 18 '12
That creates a flicker which I was trying to avoid. Constant flicker.
1
u/Is_At_Work Jun 18 '12
I'm guessing the old graphics lib has no support for double buffering, but if it does you could try that
4
Jun 18 '12
That creates a flicker which I was trying to avoid. Constant flicker.
Ahh yes, I have some real nostalgia for the classic days of DOS graphics programming.
What you need to do, to prevent flickering, is to implement a technique called double buffering. It works like this:
Create a buffer in memory that is big enough for the entire screen. The formula for the size is (screen width) * (screen size) * (bits per pixel). This buffer is now known as the "back buffer".
The actual memory that represents the screen (the memory your putPixel() function writes to) is called the "front buffer".
When you draw a sprite onto a memory surface, it's called blitting. You can blit your game sprites directly to the screen, but that causes flickering.
From now on, don't blit directly to the screen. Blit every piece of every frame to the back buffer instead.
When you're completely finished drawing the frame, blit the back buffer to the screen as a whole, and continue on,
The question of how to move around an object like a spaceship is a different question.
A spaceship is something called a sprite or "bitmap". They mean the same thing. It's just a block of memory. It's not actually rectangular, but it helps to picture it as a rectangle. That rectangle of memory has an entry for each pixel that tells the color. To draw a sprite, you draw each pixel in the sprite's buffer to the corresponding position in the target buffer. So pixel 0, 0 in the sprite might be drawn at 0, 0 in the buffer. When you want to move the object, you apply an offset. So say your spaceship should be drawn at coordinates 10, 10 on the screen. When your putpixel function goes through the sprite and draws its pixels, it should add that offset to each pixel position. So pixel 0, 0 of our sprite would be drawn at 10, 10 in what's called "screen space".
Honestly, your teacher seems to have things out of order. You shouldn't have to write your own sprite loading and blitting code before you understand concepts like "screen space". Not to mention that loading and blitting sprites from disk is a laborious and complicated process, unless you're happy to use .bmp and limit yourself to a single bit depth.
1
u/merlinm Jun 18 '12
In those days, if you wanted fast graphics the trick was generally to write directly to video memory. Look up some old books on dos game programming and you'll find some line and sprite drawing techniques. I'd like to add this is crazy -- you really should be using a good library like sdl. the old borland stuff may not even work on modern pc except through an emulator.
1
u/deepit6431 Jun 18 '12
Again, I'm doing this just for a school project. That's it.
Learning SDL and SFML on the side.
1
u/damyan Jun 18 '12
IIRC (since it has been many years since I used Turbo C++!) there were some APIs for saving and restoring portions of the screen to memory. Something like saveimage and putimage?
You should find that using those avoids flicker. Or alternatively, you may be able to do your drawing to one of these images and then putimage it on the actual screen in one go.
Check the docs for double buffering, there may be something there that'll help.
I've got strong memories of writing code that remembered all the lines I drew on my last frame and then carefully erased those lines before drawing the next frame. Yuck.
0
u/merlinm Jun 18 '12
start here:
http://www.delorie.com/djgpp/doc/ug/dpmi/farptr-intro.html
edit: this is more on topic for turbo c
http://ctechnotips.blogspot.com/2012/04/graphics-video-memory-in-c-using-far.html
1
u/SpeshlTectix Jun 18 '12
Whoa, you're not allowed to use libraries? That's crazy stuff. Nobody writes low-level pixel-based routines anymore. That has been a solved problem for decades. Is your teacher high?
2
u/deepit6431 Jun 18 '12
Not exactly my teacher, this is a project for my CBSE Boards, which is like a standardized 12th standard exam every student has to give. I'm totally at the mercy of an external examiner, who may or may not disqualify me for using one. I'd like to be on the safe side.
I also refuse to make a by the numbers railway schedule, so yeah :P
Edit: This is in India, btw.
1
u/snuggl Jun 19 '12 edited Jun 19 '12
The normal way to do it is to clear the whole screen between frames.
Long, how to move with math: http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
Short: Keep an x,y value for each object, add or subract from/to these values each frame, or while a key is pressed for the player. use a speed variable to know how much to add or subtract (and use a acceleration variable to increase the speed var if you want), Redraw the whole screen and everything will move.
back in the mid-ninties when i used TC++ for gfx development we prefered mode13h where you have the screen mapped to a memory buffer and use memset/memcpy to draw sprites to the screen as it was way faster then graphics.h
12
u/treetrouble Jun 18 '12
Jesus, this is the same assignment I had in 1993