Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #ifndef GAME_H
  2. #define GAME_H
  3.  
  4. class Game {
  5. private:
  6. SDL_Event _ev;
  7. bool _isRunning;
  8. public:
  9. Game();
  10. ~Game();
  11.  
  12. void run();
  13. };
  14.  
  15. #endif
  16.  
  17. /*
  18. * Graphics.cpp
  19. * Holds info for dealing with game's graphics
  20. *
  21. *
  22. */
  23.  
  24. #include <SDL2/SDL.h>
  25. #include <iostream>
  26.  
  27. #include "Graphics.h"
  28. #include "functions.h"
  29.  
  30. Graphics::Graphics() :
  31. _windowTitle ("SDL2 project"),
  32. _windowWidth (640),
  33. _windowHeight (480),
  34. _renderingDriverIndex (-1), // get the index of the first graphics driver available(?)
  35. _rendererFlags (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC) // use GPU, VSYNC
  36. {
  37. _window= SDL_CreateWindow(_windowTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _windowWidth, _windowHeight, SDL_WINDOW_SHOWN);
  38. if(_window == 0){
  39. printError("SDL_CreateWindow");
  40. SDL_Quit();
  41. }
  42.  
  43. _renderer= SDL_CreateRenderer(_window, _renderingDriverIndex, _rendererFlags);
  44. if(_renderer == NULL){
  45. printError("SDL_CreateRenderer");
  46. SDL_Quit();
  47. }
  48. }
  49.  
  50. Graphics::~Graphics() {
  51. SDL_DestroyWindow(_window);
  52. SDL_DestroyRenderer(_renderer);
  53. }
  54.  
  55. void Graphics::changeWindowSize(int width, int height){
  56. SDL_SetWindowSize(_window, width, height);
  57. }
  58.  
  59. SDL_Renderer* Graphics::getRenderer(){
  60. return _renderer;
  61. }
  62.  
  63. int Graphics::getWindowWidth(){
  64. return _windowWidth;
  65. }
  66.  
  67. int Graphics::getWindowHeight(){
  68. return _windowHeight;
  69. }
  70.  
  71.  
  72. Layer::Layer(SDL_Renderer* renderer, int width, int height) :
  73. _texture(SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width, height))
  74. {
  75.  
  76. }
  77.  
  78. Layer::~Layer(){
  79. SDL_DestroyTexture(_texture);
  80. }
  81.  
  82. SDL_Texture* Layer::getTexture(){
  83. return _texture;
  84. }
  85.  
  86. /*
  87. * functions.h
  88. *
  89. */
  90.  
  91. #ifndef FUNCTIONS_H
  92. #define FUNCTIONS_H
  93.  
  94. void printError(const char* errorMessage);
  95.  
  96. #endif /* FUNCTIONS_H */
  97.  
  98. /*
  99. * functions.cpp
  100. *
  101. */
  102.  
  103. #include <SDL2/SDL_log.h>
  104. #include <SDL2/SDL_error.h>
  105. #include <iostream>
  106.  
  107. #include "functions.h"
  108.  
  109. void printError(const char* errorMessage){
  110. std::cout << errorMessage << " error: " << SDL_GetError() << std::endl;
  111. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, errorMessage, " error: %sn", SDL_GetError());
  112. }
  113.  
  114. /*
  115. * main.cpp
  116. * Entry point for game
  117. *
  118. */
  119.  
  120. #include <SDL2/SDL.h>
  121. #include <iostream>
  122.  
  123. #include "Game.h"
  124. #include "functions.h"
  125.  
  126. int main(int argc, char* argv[]) {
  127. Game game;
  128. game.run();
  129.  
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement