Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.32 KB | None | 0 0
  1. #include <SDL2/SDL.h>
  2. #include <SDL2/SDL_ttf.h>
  3. #include <SDL2/SDL_image.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <math.h>
  8. #include <string.h>
  9. #include "estruturas.h"
  10.  
  11. // pre-processor definitions
  12. #define STRING_SIZE 100
  13. #define MAP_SIZE 1000
  14. #define WINDOW_POSX 500
  15. #define WINDOW_POSY 250
  16. #define EXTRASPACE 150
  17. #define MARGIN 5
  18. #define nbytes 101
  19. #define sizefn 100
  20. #define sizern 10
  21. #define OBSTACULO 2
  22. #define SUJO 0
  23. #define LIMPO 1
  24.  
  25.  
  26. // function declarations
  27. void ordenalista(robot *head,robot *tail);
  28. robot* createList(robot *head,robot *tail);
  29. robot* delBat(robot *aux,robot *head,robot *tail);
  30. void lerficheiro(FILE *fp,int nSquareH,int nSquareW,quadrados ***mat,robot *head,robot *tail);
  31. void MoveRobot(robot* aux,quadrados **mat,int nSquareH,int nSquareW);
  32. int InitEverything(int , int , SDL_Window** , SDL_Renderer** );
  33. int InitSDL();
  34. int InitFont();
  35. SDL_Window* CreateWindow(int , int );
  36. SDL_Renderer* CreateRenderer(int , int , SDL_Window* );
  37. void RenderMap(int , int , SDL_Renderer*,quadrados **mat,int ,robot*head,robot*tail);
  38. int RenderText(int , int , const char* , TTF_Font *, SDL_Renderer* );
  39. int RenderLogo(int , int , SDL_Renderer* );
  40.  
  41. // some global constant variables
  42. const char myName[] = "Miguel Nascimento";
  43. const char myName2[]= "Laura Silva";
  44. const char myNumber[] = "80785";
  45. const char myNumber2[] = "81792";
  46.  
  47.  
  48. /**
  49. * main function: entry point of the program
  50. */
  51. int main( int argc, char* args[] )
  52. {
  53. int nSquareH=0,nSquareW=0,num_obstaculos=0,num_arg=0,width=0,height=0,SQUARE_SIZE=0,area=0;
  54. quadrados **mat;
  55. int max_dropout=0,dropout=0;
  56. int delay = 1000;
  57. int key=0;
  58. SDL_Event event;
  59. SDL_Window* window = NULL;
  60. SDL_Renderer* renderer = NULL;
  61. char *line;
  62. FILE *fp;
  63. robot *head = (robot*)malloc(sizeof(robot));
  64. robot *tail = (robot*)malloc(sizeof(robot));
  65. robot *aux = head;
  66. srand(time(NULL));
  67.  
  68. if (argc!=3)
  69. {
  70. printf("Numero insuficiente de parametros!");
  71. exit(EXIT_FAILURE);
  72. }
  73.  
  74. sscanf(args[2],"%d",&max_dropout);
  75. if(max_dropout<0.1)
  76. {
  77. printf("Decrescimo de bateria demasiado baixo!");
  78. exit(EXIT_FAILURE);
  79. }
  80.  
  81. fp=fopen(args[1],"r");
  82. if (fp==NULL)
  83. {
  84. printf("O ficheiro com o nome indicado não existe!");
  85. exit(EXIT_FAILURE);
  86. }
  87.  
  88. line = (char *) malloc (nbytes * sizeof(char));
  89. fgets(line,nbytes,fp);
  90. num_arg=sscanf(line,"%d %d %d",&nSquareW,&nSquareH,&num_obstaculos);
  91. if (num_arg!=3)
  92. {
  93. printf("\nErro! Dados num formato não suportado");
  94. exit(EXIT_FAILURE);
  95. }
  96.  
  97. head=createList(head,tail);
  98. lerficheiro(fp,nSquareH,nSquareW,&mat,head,tail);
  99.  
  100. area=nSquareH*nSquareW;
  101.  
  102. if(area>250)
  103. SQUARE_SIZE=30;
  104. else
  105. SQUARE_SIZE=40;
  106.  
  107. // calculate the window size
  108. width = SQUARE_SIZE*nSquareW;
  109. height = SQUARE_SIZE*nSquareH;
  110.  
  111. // initialize graphics
  112. if ( !InitEverything(width, height, &window, &renderer) )
  113. return -1;
  114.  
  115. while( 1 )
  116. {
  117. // while there's events to handle
  118. while( SDL_PollEvent( &event ) )
  119. {
  120. if( event.type == SDL_QUIT )
  121. {
  122. // quit the program
  123. exit(EXIT_SUCCESS);
  124. }
  125. else if ( event.type == SDL_KEYDOWN )
  126. {
  127. switch ( event.key.keysym.sym )
  128. {
  129. case SDLK_DOWN:
  130. // speed-down
  131. delay=delay+50;
  132. break;
  133. case SDLK_UP:
  134. // speed-up
  135. delay=delay-50;
  136. if (delay<0)
  137. delay=0;
  138. break;
  139. case SDLK_p:
  140. //pausa a simulação
  141. key=1;
  142. while (key==1)
  143. {
  144. while( SDL_PollEvent( &event ) )
  145. {
  146. if( event.type == SDL_QUIT )
  147. {
  148. // quit the program
  149. exit(EXIT_SUCCESS);
  150. }
  151.  
  152. else if(event.type == SDL_KEYDOWN)
  153. {
  154. switch ( event.key.keysym.sym )
  155. {
  156. case SDLK_p:
  157. key=0;
  158. break;
  159.  
  160. case SDLK_a:
  161. //adicionar robo
  162. break;
  163.  
  164. case SDLK_q:
  165. //fecha a simulação
  166. exit(EXIT_SUCCESS);
  167. break;
  168.  
  169. default:
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. break;
  176. case SDLK_q:
  177. //fecha a simulação
  178. exit(EXIT_SUCCESS);
  179. break;
  180. case SDLK_i:
  181. //reinicializa a simulação
  182. free(line);
  183. for(aux=head->next;aux!=tail;aux=aux->next)
  184. {
  185. aux->prev=aux->next;
  186. free(aux);
  187. }
  188. free(mat);
  189. fp=fopen(args[1],"r");
  190. if (fp==NULL)
  191. {
  192. printf("O ficheiro com o nome indicado não existe!");
  193. exit(EXIT_FAILURE);
  194. }
  195.  
  196. line = (char *) malloc (nbytes * sizeof(char));
  197. fgets(line,nbytes,fp);
  198. num_arg=sscanf(line,"%d %d %d",&nSquareW,&nSquareH,&num_obstaculos);
  199. if (num_arg!=3)
  200. {
  201. printf("\nErro! Dados num formato não suportado");
  202. exit(EXIT_FAILURE);
  203. }
  204. head=createList(head,tail);
  205. lerficheiro(fp,nSquareH,nSquareW,&mat,head,tail);
  206.  
  207. area=nSquareH*nSquareW;
  208.  
  209. if(area>250)
  210. SQUARE_SIZE=30;
  211. else
  212. SQUARE_SIZE=40;
  213. break;
  214.  
  215. default:
  216. break;
  217. }
  218. }
  219. }
  220.  
  221. for(aux=head->next;aux!=tail;aux=aux->next)
  222. {
  223. RenderMap(nSquareW, nSquareH, renderer,mat,SQUARE_SIZE,head,tail);
  224. MoveRobot(aux,mat,nSquareH,nSquareW);
  225.  
  226. dropout=(max_dropout+0.1)*10;
  227. dropout=(rand()%dropout)/10;
  228. aux->bateria=(aux->bateria)-dropout;
  229. head=delBat(aux,head,tail);
  230. }
  231.  
  232. // add a delay
  233. SDL_Delay( delay );
  234. }
  235.  
  236. SDL_DestroyRenderer(renderer);
  237. SDL_DestroyWindow(window);
  238. SDL_Quit();
  239. return 1;
  240. }
  241.  
  242. void addrobot(robot *head, robot *tail, int SQUARE_SIZE, int nSquareW, int nSquareH)
  243. {
  244. char *novonome=(char *)malloc(3 * sizeof(char));
  245. novonome[0]='R';
  246. novonome[1]=(char)( event.key.keysym.sym );
  247. novonome[2]='\0';
  248. }
  249.  
  250. void ordenalista(robot *head,robot *tail)
  251. {
  252. robot *aux;
  253. for (aux=head->next;aux!=tail;aux=aux->next)
  254. {
  255. if(aux->prev==head)
  256. {
  257. if(aux->nome[1]>(aux->next)->nome[1])
  258. {
  259. (aux->next)->next=aux;
  260. (aux->next)->prev=head;
  261. aux->next=(aux->next)->next;
  262. }
  263. }
  264.  
  265. else
  266. {
  267. if(aux->nome[1]<(aux->prev)->nome[1])
  268. {
  269. (aux->prev)->prev=aux;
  270. (aux->prev)->next=aux->next;
  271. aux->next=aux->prev;
  272. }
  273. }
  274. if(aux->next==tail)
  275. {
  276. if(aux->nome[1]<(aux->prev)->nome[1])
  277. {
  278. (aux->prev)->prev=aux;
  279. (aux->prev)->next=tail;
  280. aux->next=aux->prev;
  281. }
  282. }
  283. }
  284. }
  285.  
  286. robot* delBat(robot *aux,robot *head,robot *tail)
  287. {
  288. if(aux->bateria<=0 && head->next!=tail)
  289. {
  290. (aux->prev)->next=aux->next;
  291. (aux->next)->prev=aux->prev;
  292. free(aux);
  293. }
  294. return head;
  295. }
  296.  
  297. void MoveRobot(robot* aux,quadrados **mat,int nSquareH,int nSquareW)
  298. {
  299. double angle;
  300. int atualx=(aux->atual).x,atualy=(aux->atual).y;
  301.  
  302.  
  303. while((aux->target).x==(aux->atual).x && (aux->target).y==(aux->atual).y) //Quando é atingido a posição destino
  304. {
  305. (aux->target).x=rand()%nSquareW;
  306. (aux->target).y=rand()%nSquareH;
  307. }
  308.  
  309.  
  310. // calculate the angle
  311. angle = atan2((double)((aux->atual).y-(aux->target).y), (double)((aux->target).x)-(aux->atual).x);
  312.  
  313. // calculate the new position
  314. (aux->atual).x = floor((aux->atual).x + cos(angle)+0.5);
  315. (aux->atual).y = floor((aux->atual).y - sin(angle)+0.5);
  316.  
  317.  
  318.  
  319. if(mat[(aux->atual).y][(aux->atual).x].estarobot==1)//Quando a próxima posição está ocupada por um robô
  320. {
  321. (aux->target).x=rand()%nSquareW;
  322. (aux->target).y=rand()%nSquareH;
  323. }
  324.  
  325. if(mat[(aux->atual).y][(aux->atual).x].estado==OBSTACULO)//Quando a próxima posição está ocupada por um obstáculo
  326. {
  327. (aux->atual).x=atualx;
  328. (aux->atual).y=atualy;
  329. (aux->target).x=rand()%nSquareW;
  330. (aux->target).y=rand()%nSquareH;
  331. }
  332.  
  333.  
  334. mat[(aux->atual).y][(aux->atual).x].estado=LIMPO;
  335. mat[(aux->atual).y][(aux->atual).x].estarobot=1;
  336.  
  337.  
  338. }
  339.  
  340. robot* createList(robot *head,robot *tail)
  341. {
  342. head->prev = head;
  343. head->next = tail;
  344. tail->prev = head;
  345. tail->next = NULL;
  346. return head;
  347. }
  348.  
  349. void lerficheiro(FILE *fp,int nSquareH,int nSquareW,quadrados***mat,robot*head,robot*tail)
  350. {
  351. char nome_robot[sizern];/*nome_robot- armazena o nome do robot */
  352. char *line;
  353. robot *aux=head;
  354. int num_arg=0;
  355. int xobst,yobst,num_robots,xrobot,yrobot,bat,i=0;
  356.  
  357. (*mat) = (quadrados**)calloc(nSquareH, sizeof(quadrados*)); //alocar memoria na matriz sobre as linhas
  358. if((*mat) == NULL)
  359. {
  360. printf("Sem memoria\n");
  361. exit(EXIT_FAILURE);
  362. }
  363. for(i = 0; i < nSquareH; i++) //alocar para cada linha
  364. {
  365. (*mat)[i] = (quadrados *)calloc(nSquareW, sizeof(quadrados));
  366. if((*mat)[i] == NULL)
  367. {
  368. printf("Sem memoria\n");
  369. exit(EXIT_FAILURE);
  370. }
  371. }
  372. line = (char *) malloc (nbytes * sizeof(char));
  373. while(fgets(line,nbytes,fp)!=NULL)
  374. {
  375. if(sscanf(line,"[%d, %d]",&xobst,&yobst))
  376. {
  377. (*mat)[yobst][xobst].estado=2;
  378. }
  379. if(sscanf(line,"%d",&num_robots))
  380. {
  381. printf("%d\n",num_robots);
  382. }
  383. num_arg=sscanf(line,"%s [%d, %d] %d",nome_robot,&xrobot,&yrobot,&bat);
  384. if(sscanf(line,"%s [%d, %d] %d",nome_robot,&xrobot,&yrobot,&bat) && num_arg==4)
  385. {
  386. (*mat)[yrobot][xrobot].estado=1;
  387. (*mat)[yrobot][xrobot].estarobot=1;
  388.  
  389. robot *node = (robot*)malloc(sizeof(robot));
  390. node->next=aux->next;
  391. (node->next)->prev=node;
  392. aux->next=node;
  393. node->prev=aux;
  394.  
  395. node->bateria=bat;
  396. (node->atual).x=xrobot;
  397. (node->atual).y=yrobot;
  398. strcpy(node->nome,nome_robot);
  399. (node->target).x=rand()%nSquareW;
  400. (node->target).y=rand()%nSquareH;
  401.  
  402. aux=aux->next;
  403.  
  404. }
  405. }
  406. fclose(fp);
  407. //ordenalista(head,tail);
  408. }
  409.  
  410.  
  411.  
  412. /**
  413. * RenderLogo function: Renders the IST Logo on the window screen
  414. * \param x X coordinate of the Logo
  415. * \param y Y coordinate of the Logo
  416. * \param _renderer renderer to handle all rendering in a window
  417. */
  418. int RenderLogo(int x, int y, SDL_Renderer* _renderer)
  419. {
  420. SDL_Texture *text_IST;
  421. SDL_Surface *img_IST;
  422. SDL_Rect boardPos;
  423.  
  424. // renders IST Logo
  425. img_IST = SDL_LoadBMP("ist_logo.bmp");
  426. if (img_IST == NULL)
  427. {
  428. printf("Unable to load bitmap: %s\n", SDL_GetError());
  429. exit(-5);
  430. }
  431. // square where the Logo is placed
  432. boardPos.x = x;
  433. boardPos.y = y;
  434. boardPos.w = img_IST->w;
  435. boardPos.h = img_IST->h;
  436.  
  437. // creates a texture and renders it in the screen
  438. text_IST = SDL_CreateTextureFromSurface(_renderer, img_IST);
  439. SDL_RenderCopy(_renderer, text_IST, NULL, &boardPos);
  440.  
  441. // destroy texture and surface
  442. SDL_DestroyTexture(text_IST);
  443. SDL_FreeSurface(img_IST);
  444. return img_IST->h;
  445. }
  446.  
  447. /**
  448. * RenderText function: Renders the IST Logo on the window screen
  449. * \param x X coordinate of the text
  450. * \param y Y coordinate of the text
  451. * \param text string where the text is written
  452. * \param font TTF font used to render the text
  453. * \param _renderer renderer to handle all rendering in a window
  454. */
  455. int RenderText(int x, int y, const char *text, TTF_Font *font, SDL_Renderer* _renderer)
  456. {
  457. SDL_Color color = { 0, 0, 0 };
  458. SDL_Surface *text_surface;
  459. SDL_Texture *text_texture;
  460. SDL_Rect solidRect;
  461.  
  462. solidRect.x = x;
  463. solidRect.y = y;
  464.  
  465. // creates a surface with some text
  466. text_surface = TTF_RenderText_Blended(font,text,color);
  467. if(!text_surface)
  468. {
  469. printf("TTF_RenderText_Blended: %s\n", TTF_GetError());
  470. exit(-5);
  471. }
  472.  
  473. // creates a texture from the surface and renders it !
  474. text_texture = SDL_CreateTextureFromSurface(_renderer, text_surface);
  475. SDL_QueryTexture( text_texture, NULL, NULL, &solidRect.w, &solidRect.h );
  476. SDL_RenderCopy(_renderer, text_texture, NULL, &solidRect);
  477.  
  478. // destroy texture and surface
  479. SDL_DestroyTexture(text_texture);
  480. SDL_FreeSurface(text_surface);
  481. return solidRect.h;
  482. }
  483.  
  484. /**
  485. * RenderMap function: Renders the map on the window screen according to their size
  486. * \param nSquareW number of squares to render (width)
  487. * \param nSquareH number of squares to render (height)
  488. * \param pos_robot position of the robot (ID of the square)
  489. * \param robot_name name of the robot (max. 2 letters)
  490. * \param _renderer renderer to handle all rendering in a window
  491. */
  492. void RenderMap(int nSquareW, int nSquareH, SDL_Renderer* _renderer,quadrados**mat, int SQUARE_SIZE,robot *head,robot *tail)
  493. {
  494. TTF_Font *sans;
  495. TTF_Font *serif;
  496. SDL_Rect gridPos;
  497. int i,j, height;
  498. robot *aux=head;
  499. char bateria[3];
  500.  
  501. // set color of renderer to some color
  502. SDL_SetRenderDrawColor( _renderer, 255, 255, 255, 255 );
  503.  
  504. // clear the window
  505. SDL_RenderClear( _renderer );
  506.  
  507. // opens a font style and sets a size
  508. sans = TTF_OpenFont("FreeSans.ttf", 24);
  509. serif = TTF_OpenFont("FreeSerif.ttf", 16);
  510. if(!sans || !serif)
  511. {
  512. printf("TTF_OpenFont: %s\n", TTF_GetError());
  513. exit(-5);
  514. }
  515. // render the IST Logo
  516. height = RenderLogo(nSquareW*SQUARE_SIZE, 0, _renderer);
  517.  
  518. // render the student name
  519. height += RenderText(nSquareW*SQUARE_SIZE+3*MARGIN, height, myName, serif, _renderer);
  520. height += RenderText(nSquareW*SQUARE_SIZE+3*MARGIN, height, myName2, serif, _renderer);
  521.  
  522. // render the student number
  523. height += RenderText(nSquareW*SQUARE_SIZE+3*MARGIN, height, myNumber, serif, _renderer);
  524. height += RenderText(nSquareW*SQUARE_SIZE+3*MARGIN, height, myNumber2, serif, _renderer);
  525.  
  526.  
  527. for (aux=head->next;aux!=tail;aux=aux->next)
  528. {
  529. sprintf(bateria,"%s:%d",aux->nome,aux->bateria);
  530. height += RenderText(nSquareW*SQUARE_SIZE+3*MARGIN, height,bateria, serif, _renderer);
  531.  
  532. }
  533.  
  534. // grid position
  535. gridPos.w = SQUARE_SIZE;
  536. gridPos.h = SQUARE_SIZE;
  537. gridPos.y = 0;
  538.  
  539. // iterate over all squares
  540. for (i = 0; i < nSquareH; i++)
  541. {
  542. gridPos.x = 0;
  543. for (j = 0; j < nSquareW; j++)
  544. {
  545. if (mat[i][j].estado==SUJO)
  546. {
  547. // writes a dirty square
  548. SDL_SetRenderDrawColor( _renderer, 221, 161, 135, 255 );
  549. SDL_RenderFillRect( _renderer, &gridPos );
  550. SDL_SetRenderDrawColor( _renderer, 0, 0, 0, 255 );
  551. SDL_RenderDrawRect( _renderer, &gridPos );
  552. }
  553. else if (mat[i][j].estado==LIMPO)
  554. {
  555. // cleans a dirty square
  556. SDL_SetRenderDrawColor( _renderer, 255, 255, 255, 255 );
  557. SDL_RenderFillRect( _renderer, &gridPos );
  558. SDL_SetRenderDrawColor( _renderer, 0, 0, 0, 255 );
  559. SDL_RenderDrawRect( _renderer, &gridPos );
  560. }
  561. else if (mat[i][j].estado==OBSTACULO)
  562. {
  563. //coloca um obstaculo
  564. SDL_SetRenderDrawColor( _renderer, 0, 0, 0, 255 );
  565. SDL_RenderFillRect( _renderer, &gridPos );
  566. SDL_SetRenderDrawColor( _renderer, 0, 0, 0, 255 );
  567. SDL_RenderDrawRect( _renderer, &gridPos );
  568.  
  569. }
  570.  
  571. for(aux=head->next;aux!=tail;aux=aux->next)
  572. {
  573.  
  574. if((aux->target).x==j && (aux->target).y==i)
  575. {
  576. //coloca o target
  577. SDL_SetRenderDrawColor( _renderer, 25, 25, 112, 255 );
  578. SDL_RenderFillRect( _renderer, &gridPos );
  579. SDL_SetRenderDrawColor( _renderer, 0, 0, 0, 255 );
  580. SDL_RenderDrawRect( _renderer, &gridPos );
  581.  
  582. }
  583. if((aux->atual).x==j && (aux->atual).y==i)
  584. {
  585. RenderText(gridPos.x+MARGIN, gridPos.y, aux->nome, sans, _renderer);
  586. }
  587. }
  588. gridPos.x += SQUARE_SIZE;
  589. }
  590. gridPos.y += SQUARE_SIZE;
  591. }
  592.  
  593. // render the changes above
  594. SDL_RenderPresent( _renderer);
  595.  
  596. // destroy everything
  597. TTF_CloseFont(sans);
  598. TTF_CloseFont(serif);
  599. }
  600.  
  601.  
  602. /**
  603. * InitEverything: Initializes the SDL2 library and all graphical components: font, window, renderer
  604. * \param width width in px of the window
  605. * \param height height in px of the window
  606. * \param _window represents the window of the application
  607. * \param _renderer renderer to handle all rendering in a window
  608. */
  609. int InitEverything(int width, int height, SDL_Window** _window, SDL_Renderer** _renderer)
  610. {
  611. SDL_Window* window = NULL;
  612. SDL_Renderer* renderer = NULL;
  613.  
  614. if ( !InitSDL() )
  615. return 0;
  616.  
  617. if ( !InitFont() )
  618. return 0;
  619.  
  620. window = CreateWindow(width, height);
  621. if ( window == NULL )
  622. return 0;
  623.  
  624. renderer = CreateRenderer(width, height, window);
  625. if ( renderer == NULL )
  626. return 0;
  627.  
  628. *_window = window;
  629. *_renderer = renderer;
  630.  
  631. return 1;
  632. }
  633.  
  634. /**
  635. * InitSDL: Initializes the SDL2 graphic library
  636. */
  637. int InitSDL()
  638. {
  639. // init SDL library
  640. if ( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
  641. {
  642. printf(" Failed to initialize SDL : %s\n", SDL_GetError());
  643. return 0;
  644. }
  645.  
  646. return 1;
  647. }
  648.  
  649. /**
  650. * InitFont: Initializes the SDL2_ttf font library
  651. */
  652. int InitFont()
  653. {
  654. // init font library
  655. if(TTF_Init()==-1)
  656. {
  657. printf("TTF_Init: %s\n", TTF_GetError());
  658. return 0;
  659. }
  660.  
  661. return 1;
  662. }
  663.  
  664. /**
  665. * CreateWindow: Creates a window for the application
  666. * \param width width in px of the window
  667. * \param height height in px of the window
  668. * \return pointer to the window created
  669. */
  670. SDL_Window* CreateWindow(int width, int height)
  671. {
  672. SDL_Window* window = NULL;
  673. // init window
  674. window = SDL_CreateWindow( "iClean", WINDOW_POSX, WINDOW_POSY, width+EXTRASPACE, height, 0 );
  675.  
  676. if ( window == NULL )
  677. {
  678. printf("Failed to create window : %s\n", SDL_GetError());
  679. return NULL;
  680. }
  681.  
  682. return window;
  683. }
  684.  
  685. /**
  686. * CreateRenderer: Creates a renderer for the application
  687. * \param width width in px of the window
  688. * \param height height in px of the window
  689. * \param _window represents the window for which the renderer is associated
  690. * \return pointer to the renderer created
  691. */
  692. SDL_Renderer* CreateRenderer(int width, int height, SDL_Window* _window)
  693. {
  694. SDL_Renderer* renderer;
  695. // init renderer
  696. renderer = SDL_CreateRenderer( _window, -1, 0 );
  697.  
  698. if ( renderer == NULL )
  699. {
  700. printf("Failed to create renderer : %s", SDL_GetError());
  701. return NULL;
  702. }
  703. // set size of renderer to the same as window
  704. SDL_RenderSetLogicalSize( renderer, width+EXTRASPACE, height );
  705.  
  706. return renderer;
  707. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement