Advertisement
Guack

SDL window

Jul 9th, 2020
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. #include <SDL2/SDL.h>
  2. #include <SDL2/SDL_image.h>
  3. #include <SDL2/SDL_mixer.h>
  4. #include <iostream>
  5. #include <vector>
  6. #define win_width 600
  7. #define win_height 400
  8. #define fps 60
  9.  
  10. using namespace std;
  11.  
  12. void cap_framerate(Uint32 starting_tick){
  13. if((1000 / fps ) > SDL_GetTicks() - starting_tick){
  14. SDL_Delay(1000 / fps -(SDL_GetTicks() - starting_tick));
  15. }
  16. }
  17.  
  18. class Sprite{
  19. protected:
  20. SDL_Surface *image;
  21. SDL_Rect rect;
  22. int origin_x, origin_y;
  23.  
  24. public:
  25. Sprite(Uint32 color, int x, int y, int w =48, int h = 64){
  26. image = SDL_CreateRGBSurface(0,w,h,32,0,0,0,0);
  27.  
  28. SDL_FillRect(image,NULL, color);
  29. rect = image->clip_rect;
  30. origin_x = rect.w / 2;
  31. origin_y = rect.h / 2;
  32.  
  33. rect.x = x;
  34. rect.y = y;
  35. }
  36. void update(){
  37.  
  38. }
  39. void draw(SDL_Surface *destination){
  40. SDL_BlitSurface(image, NULL, destination, &rect);
  41. }
  42. SDL_Surface* get_image() const{
  43. return image;
  44. }
  45. bool operator==(const Sprite &other) const {
  46. return(image == other.get_image());
  47. }
  48. };
  49.  
  50. class SpriteGroup{
  51. private:
  52. vector <Sprite*> sprites;
  53. int sprites_size;
  54.  
  55. public:
  56. SpriteGroup copy(){
  57. SpriteGroup new_group;
  58. for (int i = 0; i< sprites_size;i++){
  59. new_group.add(sprites[i]);
  60. }
  61. return new_group;
  62. }
  63. void add( Sprite *sprite ){
  64. sprites.push_back(sprite);
  65. sprites_size = sprites.size();
  66. }
  67. void remove(Sprite sprite_object){
  68. for(int i=0; i<sprites_size;i++){
  69. if( *sprites[i] == sprite_object){
  70. sprites.erase(sprites.begin() + i);
  71. }
  72. }
  73. sprites_size = sprites.size();
  74. }
  75. bool has(Sprite sprite_object){
  76. for(int i=0; i<sprites_size;i++){
  77. if( *sprites[i] == sprite_object){
  78. return true;
  79. }
  80. return false;
  81. }
  82. }
  83. void update(){
  84. if (!sprites.empty()){
  85. for(int i=0; i<sprites_size;i++){
  86. sprites[i]->update();
  87. }
  88. }
  89. }
  90. void draw(SDL_Surface *destination){
  91. if (!sprites.empty()){
  92. for(int i=0; i<sprites_size;i++){
  93. sprites[i]->draw(destination);
  94. }
  95. }
  96. }
  97.  
  98. void empty(){
  99. sprites.clear();
  100. sprites_size = sprites.size();
  101. }
  102. int size(){
  103. return sprites_size;
  104. }
  105.  
  106. vector <Sprite*> get_sprites(){
  107. return sprites;
  108. }
  109. };
  110.  
  111. class Block : public Sprite{
  112. public:
  113. Block(Uint32 color, int x, int y, int w =48, int h = 64):
  114. Sprite(color,x,y,w,h){
  115. update_properties();
  116. }
  117. void update_properties(){
  118. origin_x=0;
  119. origin_y=0;
  120.  
  121. set_position(rect.x,rect.y);
  122. }
  123. void set_position(int x,int y){
  124. rect.x = x - origin_x;
  125. rect.y = y - origin_y;
  126. }
  127. void set_image(const char filename[] = NULL){
  128. if(filename != NULL){
  129. SDL_Surface *loaded_image = NULL;
  130. loaded_image = IMG_Load(filename);
  131. if(loaded_image != NULL){
  132. image = loaded_image;
  133.  
  134. int old_x = rect.x;
  135. int old_y = rect.y;
  136.  
  137. rect = image->clip_rect;
  138.  
  139. rect.x = old_x;
  140. rect.y = old_y;
  141.  
  142. update_properties();
  143. }
  144. }
  145. }
  146.  
  147.  
  148. };
  149.  
  150. int main(int argc, char* argv[])
  151. {
  152. SDL_Init( SDL_INIT_EVERYTHING);
  153.  
  154. SDL_Window *win = NULL;
  155.  
  156. win = SDL_CreateWindow("Sup", SDL_WINDOWPOS_UNDEFINED,
  157. SDL_WINDOWPOS_UNDEFINED, win_width,win_height,
  158. SDL_WINDOW_RESIZABLE);
  159.  
  160. if (win == NULL){
  161. cout << "window error "<<endl<<SDL_GetError()<<endl;
  162. }
  163.  
  164. SDL_Surface *screen = SDL_GetWindowSurface( win );
  165. Uint32 white = SDL_MapRGB(screen->format, 255,255,255);
  166. Uint32 red = SDL_MapRGB(screen->format, 255,0,0);
  167. Uint32 blue = SDL_MapRGB(screen->format, 0,0,255);
  168. SDL_FillRect(screen,NULL,white);
  169. //Sprite object(red, win_width/2, win_height/2);
  170. //Sprite another(blue, win_width/2 -100, win_height/2 +20);
  171.  
  172. Block block(red,0,0);
  173. //block.set_image("sprite.bmp");
  174.  
  175.  
  176. SpriteGroup active_sprites;
  177. active_sprites.add(&block);
  178.  
  179. active_sprites.draw(screen);
  180.  
  181. SDL_UpdateWindowSurface(win);
  182.  
  183.  
  184. Uint32 starting_tick;
  185. SDL_Event event;
  186. bool running = true;
  187. while(running){
  188. starting_tick = SDL_GetTicks();
  189. while(SDL_PollEvent(&event)){
  190. if( event.type == SDL_QUIT){
  191. running = false;
  192. break;
  193. }
  194.  
  195. }
  196. cap_framerate(starting_tick);
  197. }
  198.  
  199.  
  200. //SDL_Delay(5000);
  201. SDL_DestroyWindow(win);
  202.  
  203. SDL_Quit();
  204. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement