main.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <stdio.h>
  2. #include "SDL.h"
  3. //Screen size variables
  4. const int SCREEN_WIDTH = 640;
  5. const int SCREEN_HEIGHT = 480;
  6. int main(int argc, char *argv[]){
  7. //Init SDL stuff
  8. SDL_Init(SDL_INIT_VIDEO);
  9. //SDL Window creation
  10. SDL_Window *window = SDL_CreateWindow(
  11. "SDL2Test",
  12. SDL_WINDOWPOS_UNDEFINED,
  13. SDL_WINDOWPOS_UNDEFINED,
  14. SCREEN_WIDTH,
  15. SCREEN_HEIGHT,
  16. 0
  17. );
  18. //SDL rendering init
  19. SDL_Surface *screen;
  20. screen = SDL_GetWindowSurface(window);
  21. //Main loop
  22. bool isRunning = true;
  23. while(isRunning){
  24. //Manage Events
  25. SDL_Event test_event;
  26. while(SDL_PollEvent(&test_event)){
  27. if(test_event.type == SDL_QUIT){
  28. isRunning = false;
  29. }
  30. }
  31. SDL_LockSurface(screen);
  32. //Clear Pixels
  33. int memSize = screen->h * screen->pitch;
  34. int size = screen->h * screen->w;
  35. SDL_memset(screen->pixels, 0, memSize );
  36. printf("Memset completed.\n");
  37. printf("Pixel Array size %d.\n", size);
  38. printf("Pixel memory size %d.\n", memSize);
  39. //Modify pixels
  40. Uint32 *pixels =(Uint32 *)screen->pixels;
  41. for(int i = 0; i < size; i++){
  42. if ((i % 10) == 0){
  43. //printf("Setting Value %d.\n", i);
  44. pixels[i] = 0xFF0000;
  45. }
  46. }
  47. SDL_UnlockSurface(screen);
  48. //Updating the screen
  49. SDL_UpdateWindowSurface(window);
  50. SDL_Delay(1000);
  51. }
  52. //Clean up
  53. SDL_DestroyWindow(window);
  54. SDL_Quit();
  55. return 0;
  56. }