Ugh! I'm beginning to hate this course

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 57
Reputation: RayvenHawk is an unknown quantity at this point 
Solved Threads: 1
RayvenHawk RayvenHawk is offline Offline
Junior Poster in Training

Ugh! I'm beginning to hate this course

 
0
  #1
Jan 17th, 2009
Ok I have a problem here. I'm in my game engine class and for some unknown forsaken reason they decided to teach console c++ then jump to c# for windows apps, then back to c++ to do directx apps, so my skills with c++ and windows apps is well krappy. So I'm trying to learn as quick as possible.

Anyway on to my question, this weeks assignment is we need to load up a bitmap (already did that) and have it float around the screen slowly. I'm only able to get it to constantly load the image so it remebles more of an asteroid belt than a single rock floating in the window.

I'm assuming that DrawBitmap is probably the wrong way to go with this, but not sure. What function do I need to call to acomplish getting the image to move around the screen?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Ugh! I'm beginning to hate this course

 
0
  #2
Jan 18th, 2009
BitBlt?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,867
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 120
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Ugh! I'm beginning to hate this course

 
0
  #3
Jan 18th, 2009
You just need to manipulate the coordinates of the frame where your are displaying the image.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 57
Reputation: RayvenHawk is an unknown quantity at this point 
Solved Threads: 1
RayvenHawk RayvenHawk is offline Offline
Junior Poster in Training

Re: Ugh! I'm beginning to hate this course

 
0
  #4
Jan 18th, 2009
Here the code I currently have for the image. What it does right now is flashes new images on the screen instead of making the single image float around the screen.

  1. void DrawBitmap(HDC hdcDest, char *filename, int x, int y)
  2. {
  3. HBITMAP image;
  4. BITMAP bm;
  5. HDC hdcMem;
  6.  
  7. //load the bitmap image
  8. image = (HBITMAP)LoadImage(0,"asteroid.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
  9.  
  10. //read the bitmap's properties
  11. GetObject(image, sizeof(BITMAP), &bm);
  12.  
  13. //create a device context for the bitmap
  14. hdcMem = CreateCompatibleDC(global_hdc);
  15. SelectObject(hdcMem, image);
  16.  
  17. //draw the bitmap to the window (bit block transfer)
  18. BitBlt(
  19. global_hdc, //destination device context
  20. x, y, //x,y location on destination
  21. bm.bmWidth, bm.bmHeight, //width,height of source bitmap
  22. hdcMem, //source bitmap device context
  23. 0, 0, //start x,y on source bitmap
  24. SRCCOPY); //blit method
  25.  
  26. //delete the device context and bitmap
  27. DeleteDC(hdcMem);
  28. DeleteObject((HBITMAP)image);
  29. }
  30.  
  31.  
  32. void Game_Init()
  33. {
  34. //initialize the game...
  35. //load bitmaps, meshes, textures, sounds, etc.
  36.  
  37. srand(time(NULL));
  38. }
  39.  
  40. void Game_Run()
  41. {
  42. //this is called once every frame
  43. //do not include your own loop here!
  44.  
  45. int x = 0, y = 0;
  46. RECT rect;
  47. GetClientRect(global_hwnd, &rect);
  48.  
  49. if (rect.right > 0)
  50. {
  51. x = rand() % (rect.right - rect.left);
  52. y = rand() % (rect.bottom - rect.top);
  53. DrawBitmap(global_hdc, "asteroid.bmp", x, y);
  54. }
  55. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 640
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 104
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Ugh! I'm beginning to hate this course

 
0
  #5
Jan 18th, 2009
I was going to recommend that you only load the bitmap once, but I noticed that your code already has a comment about it:
  1. //initialize the game...
  2. //load bitmaps, meshes, textures, sounds, etc.

And your whole x = rand()..., y = rand() appears to be all about picking a random location on the screen to draw at.

From what you described the desired behavior to be, you want to start your image somewhere (maybe the center?). Then you select a random direction (as dx, dy) for it to start moving. Then on each pass of the loop, you calculate the 'new position' of the image from the old by applying dx, dy and doing edge testing. (Edge testing is where you determine if the new position would cause any portion of your image to fall 'outside' the screen -- an alternative edge test if you don't mind some of your image off-screen is to edge test for the center of your image rather than the outside edges) If you have hit a screen edge, invert the direction in relation to that edge. For example if dy was <0 and we 'hit' the Y 0 edge of the screen, you make dy=-dy (which makes it positive) and re-calculate the new position. Once you're "happy" with the new position, you use BitBlt to move the image from where it was to the new position. The whole 'float about the screen' thing happens as your image 'bounces' off the edges of the screen.

If any of that makes sense to you, apply it and see what it does. If it still doesn't work like you want, post your new code, and question in the form "I wanted it to do ____ but it is doing ____. I've tried ___ but it didn't work."

PS- When posting c++ code, please use c++ code tags
[code=c++]
// Your code here
[/code]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 307 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC