Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #include <allegro.h>
  2. #include <cmath>
  3. #include <stdlib.h>
  4. #include <time.h> //these are the header files it inherits
  5. #include <vector>
  6. #include <string>
  7. #include <sstream>
  8. #include <iostream>
  9.  
  10. //this is how allegro draws stuff to the screen:
  11. // all objects have their own position ->>>> draw each objects bitmap to a 'back buffer' this is so it doesn't draw to the screen before everything has been rendered
  12. // ->>>>> next it will draw this bitmap directly to the screen
  13. // if you don't do it this way your screen will just constantly be flashing every time an object is drawn to it
  14.  
  15.  
  16.  
  17.  
  18. int setUpAllegro();
  19.  
  20. int main()
  21. {
  22. setUpAllegro(); //calls the set up allegro function
  23.  
  24. BITMAP *buffer;
  25. BITMAP *test;
  26.  
  27. srand(time(NULL)); //initliases the random call
  28.  
  29. test = load_bitmap("NAME.bmp", NULL);
  30.  
  31. buffer = create_bitmap(SCREEN_W, SCREEN_H); //creates the buffer to stop screen flicker
  32.  
  33.  
  34. while (!key[KEY_ESC]) //this is the main game loop where all the different game functions will occur
  35. {
  36. clear_to_color(buffer, 0); //clears the buffer to colour black
  37.  
  38. masked_blit(test, buffer, 0, 0, mouse_x, mouse_y, 32, 32); //this function draws the bitmap image to the back buffer where the mouse position is. The 32 values are the size of the bitmap;
  39.  
  40. //mouse_x is mouse x position
  41. //mouse_y is mouse y position
  42. //mouse_b & 1 is left click
  43. //mouse_b & 2 is right click
  44. //key[KEY_W] gets if the w key is pressed
  45.  
  46. int bitmapX, bitmapY;
  47.  
  48. //for example
  49.  
  50. if(key[KEY_W])
  51. {
  52. bitmapX += 1;
  53. blit(test, buffer, 0, 0, bitmapX, bitmapY, 10, 10); //this will basivcally draw a bitmap to the screen if the key w is pressed. It will draw the bitmap at the bitmapX&Y positions. As it is not masked blit it will not remove any magenta on the bitmap. 10, 10 is the size of the bitmap.
  54.  
  55. }
  56.  
  57. //key[KEY_UP] -- this is for the up arrow
  58.  
  59.  
  60.  
  61. //once you have blit or masked blit or the objects you want to display on the screen then you:
  62.  
  63. blit(buffer, screen, 0, 0, 0, 0, SCREEn_W, SCREEN_H); //this draws the back buffer to the screen
  64.  
  65.  
  66.  
  67. }
  68. allegro_exit(); //exits allegro
  69. return 0;
  70. }
  71.  
  72. END_OF_MAIN();
  73.  
  74. int setUpAllegro() //sets up allegro
  75. {
  76. int retval = 0;
  77. int ret;
  78.  
  79. set_color_depth(32);
  80.  
  81. if (allegro_init() != 0)
  82. {
  83. retval = -1;
  84. }
  85.  
  86. else
  87. {
  88. install_keyboard();
  89. install_timer(); //installs the required functions
  90. install_mouse();
  91. install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, "A");
  92. ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 880, 600, 0, 0);
  93. if (ret != 0)
  94. {
  95. allegro_message(allegro_error);
  96. retval = -2;
  97. }
  98. else
  99. {
  100. textprintf(screen, font, 0, 0, makecol(255, 255, 255), "%dx%d", SCREEN_W, SCREEN_H);
  101.  
  102. }
  103. }
  104. return retval;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement