Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <SDL.h>
  4. #include "drawline.h"
  5. #include "triangle.h"
  6. #include "teapot_data.h"
  7.  
  8. triangle_t exampletriangle1 = {
  9. .x1 = 100,
  10. .y1 = 200,
  11. .x2 = 200,
  12. .y2 = 100,
  13. .x3 = 300,
  14. .y3 = 300,
  15. .fillcolor = 0xffff0000,
  16. .scale = 1.0
  17. };
  18.  
  19. triangle_t exampletriangle2 = {
  20. .x1 = 50,
  21. .y1 = 150,
  22. .x2 = 150,
  23. .y2 = 50,
  24. .x3 = 250,
  25. .y3 = 250,
  26. .fillcolor = 0xffffff00,
  27. .scale = 1.0
  28. };
  29.  
  30. triangle_t exampletriangle3 = {
  31. .x1 = 350,
  32. .y1 = 350,
  33. .x2 = 460,
  34. .y2 = 300,
  35. .x3 = 500,
  36. .y3 = 400,
  37. .fillcolor = 0xff00ff00,
  38. .scale = 1.0
  39. };
  40.  
  41. triangle_t exampletriangle4 = {
  42. .x1 = 350,
  43. .y1 = 100,
  44. .x2 = 450,
  45. .y2 = 50,
  46. .x3 = 500,
  47. .y3 = 200,
  48. .fillcolor = 0xff0000ff,
  49. .scale = 1.0
  50. };
  51.  
  52. int main()
  53. {
  54. const size_t bufsize = 100;
  55.  
  56. /* Change the screen width and height to your own liking */
  57. const int screen_w = 1600;
  58. const int screen_h = 900;
  59.  
  60. char errmsg[bufsize];
  61. int done;
  62. SDL_Surface *surface;
  63. SDL_Window *window;
  64. SDL_Event event;
  65.  
  66. /* Initialize SDL */
  67. if (SDL_Init(SDL_INIT_VIDEO) < -1) {
  68. snprintf(errmsg, bufsize, "Unable to initialize SDL.");
  69. goto error;
  70. }
  71.  
  72. /* Create a 1600x900 window */
  73. window = SDL_CreateWindow("The Amazing Teapot",
  74. SDL_WINDOWPOS_UNDEFINED,
  75. SDL_WINDOWPOS_UNDEFINED,
  76. screen_w, screen_h,
  77. 0);
  78. if(!window) {
  79. snprintf(errmsg, bufsize, "Unable to get video surface.");
  80. goto error;
  81. }
  82.  
  83. /* Create the suface in RAM that we manipulate the pixels of */
  84. surface = SDL_GetWindowSurface(window);
  85. if(!surface) {
  86. snprintf(errmsg, bufsize, "Unable to create surface.");
  87. goto error;
  88. }
  89.  
  90. /*
  91. * The teapot is represented as an array of triangle data structures.
  92. * To draw it on the screen you need to traverse the 'teapot_model' array
  93. * and call draw_triangle for each triangle (teapot_data.h contains the array).
  94. * The definition TEAPOT_NUMTRIANGLES specifies the number of triangles in the array.
  95. * The teapot model is contained within a 1000x1000 box (coordinates
  96. * from -500 to 500 on the x and y axis). Remember to translate the
  97. * model to the middle of the screen before drawing it (initialize
  98. * triangle->tx and triangle->ty with the appropriate coordinates).
  99. */
  100.  
  101. /*
  102. * Draw some example triangles on the screen.
  103. * Use these examples in the beginning.
  104. *
  105. * Remove these and draw the triangles that represent he teapot
  106. */
  107. draw_triangle(surface, &exampletriangle1);
  108. draw_triangle(surface, &exampletriangle2);
  109. draw_triangle(surface, &exampletriangle3);
  110. draw_triangle(surface, &exampletriangle4);
  111.  
  112.  
  113. /* Wait for the user to exit the application */
  114. done = 0;
  115. while (!done) {
  116.  
  117. /* Update the window surface */
  118. SDL_UpdateWindowSurface(window);
  119.  
  120. while (SDL_PollEvent(&event)) {
  121. switch(event.type) {
  122. case SDL_QUIT:
  123. done = 1;
  124. break;
  125. case SDL_WINDOWEVENT:
  126. if (event.window.event == SDL_WINDOWEVENT_SHOWN)
  127. SDL_SetWindowPosition(window, 50, 50);
  128. break;
  129. }
  130. }
  131. }
  132.  
  133. SDL_Quit();
  134.  
  135. return 0;
  136.  
  137. /* Upon an error, print message and quit properly */
  138. error:
  139. fprintf(stderr, "%s Error returned: %s\n", errmsg, SDL_GetError());
  140. SDL_Quit();
  141. exit(EXIT_FAILURE);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement