displayManager.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // ===============================
  2. // AUTHOR : Angel Ortiz (angelo12 AT vt DOT edu)
  3. // CREATE DATE : 2018-07-10
  4. // ===============================
  5. //Includes
  6. #include "displayManager.h"
  7. #include <cstdio>
  8. #include <cstring>
  9. //Dummy constructors/destructors
  10. DisplayManager::DisplayManager(){}
  11. DisplayManager::~DisplayManager(){}
  12. //Initializes the window and obtains the surface on which we draw.
  13. //Also first place where SDL is initialized.
  14. bool DisplayManager::startUp(){
  15. bool success = true;
  16. if( !startSDL() ){
  17. success = false;
  18. }
  19. else{
  20. if( !createWindow() ){
  21. success = false;
  22. }
  23. else{
  24. if( !createScreenSurface() ){
  25. success = false;
  26. }
  27. }
  28. }
  29. return success;
  30. }
  31. //Closes down sdl and destroys window.
  32. //SDL surface is also destroyed in the call to destroy window
  33. void DisplayManager::shutDown(){
  34. SDL_DestroyWindow(mWindow);
  35. mWindow = nullptr;
  36. SDL_Quit();
  37. }
  38. //Applies the rendering results to the window screen by copying the pixelbuffer values
  39. //to the screen surface.
  40. void DisplayManager::swapBuffers(Buffer<Uint32> *pixels){
  41. //Allows surface editing
  42. SDL_LockSurface(mSurface);
  43. //Copy pixels buffer resuls to screen surface
  44. std::memcpy(mSurface->pixels, pixels->buffer, pixels->mHeight*pixels->mPitch);
  45. SDL_UnlockSurface(mSurface);
  46. //Apply surface changes to window
  47. SDL_UpdateWindowSurface(mWindow);
  48. }
  49. //Entry point to SDL
  50. bool DisplayManager::startSDL(){
  51. if( SDL_Init(SDL_INIT_VIDEO) != 0){
  52. printf("Failed to initialize SDL. Error: %s\n", SDL_GetError() );
  53. return false;
  54. }
  55. return true;
  56. }
  57. //Inits window with the display values crated at compile time
  58. bool DisplayManager::createWindow(){
  59. mWindow = SDL_CreateWindow( "SoftwareRenderer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
  60. if( mWindow == nullptr){
  61. printf("Could not create window. Error: %s\n", SDL_GetError() );
  62. return false;
  63. }
  64. return true;
  65. }
  66. //Gets the screen surface
  67. //I know this is "Old" SDL and it's not really recommended anymore
  68. //But given that I am doing 100% cpu based rendering it makes sense
  69. //After all I'm not usin any of the new functionality
  70. bool DisplayManager::createScreenSurface(){
  71. mSurface = SDL_GetWindowSurface(mWindow);
  72. if(mSurface == NULL){
  73. printf("Could not create window surface. Error: %s\n", SDL_GetError());
  74. return false;
  75. }
  76. return true;
  77. }