Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #define SDL_MAIN_HANDLED
  2. #include <iostream>
  3. #include <SDL.h>
  4. using namespace std;
  5.  
  6. SDL_Texture *carregaTextura(const char *nome, SDL_Renderer *render){
  7. SDL_Surface *surface = SDL_LoadBMP(nome);
  8. SDL_Texture *textura = SDL_CreateTextureFromSurface(render, surface);
  9. SDL_FreeSurface(surface);
  10. return textura;
  11. }
  12.  
  13. bool desenhaXO(short (&mat)[3][3], int jog){
  14. int mouseX, mouseY;
  15. SDL_GetMouseState(&mouseX, &mouseY);
  16. if(mat[(mouseY / 191)][(mouseX / 203)] == -1){
  17. mat[(mouseY / 191)][(mouseX / 203)] = jog;
  18. return true;
  19. }
  20. //cout << (mouseY / 575)*3 << ", " << (mouseX / 611)*3 << "\n";
  21. return false;
  22. }
  23.  
  24. int main(){
  25. SDL_Window *mainWindow = SDL_CreateWindow("Jogo da Velha!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 611, 575, 0);
  26. SDL_Renderer *render = SDL_CreateRenderer(mainWindow, -1, SDL_RENDERER_ACCELERATED);
  27. SDL_Texture *tabuleiro = carregaTextura("img/tabuleiro.bmp", render);
  28. SDL_Texture *imgX = carregaTextura("img/X.bmp", render);
  29. SDL_Texture *imgO = carregaTextura("img/0.bmp", render);
  30. short mat[3][3] = {{-1, -1, -1},
  31. {-1, -1, -1},
  32. {-1, -1, -1}};
  33. int jogador = 0;
  34. bool end = false;
  35. while(!end){
  36. SDL_Event event;
  37. while(SDL_PollEvent(&event)){
  38. switch(event.type){
  39. case SDL_QUIT:
  40. end = true;
  41. break;
  42. case SDL_MOUSEBUTTONDOWN:
  43. desenhaXO(mat, jogador);
  44. jogador = (jogador + 1) % 2;
  45. for(int y = 0; y < 3; y++){
  46. for(int x = 0; x < 3; x++){
  47. cout << mat[y][x];
  48. }
  49. cout << "\n";
  50. }
  51. cout << "\n\n";
  52. break;
  53. }
  54. }
  55. SDL_RenderCopy(render, tabuleiro, nullptr, nullptr);
  56. for(int y = 0; y < 3; y++){
  57. for(int x = 0; x < 3; x++){
  58. SDL_Rect XOPos = {x * 203 + 50, y * 191 + 50, 71, 71};
  59. if(mat[y][x] == 1){
  60. SDL_RenderCopy(render, imgX, nullptr, &XOPos);
  61. } else if(mat[y][x] == 0){
  62. SDL_RenderCopy(render, imgO, nullptr, &XOPos);
  63. }
  64. }
  65. }
  66.  
  67. SDL_RenderPresent(render);
  68. SDL_Delay(1000/60);
  69. }
  70. SDL_DestroyTexture(tabuleiro);
  71. SDL_DestroyTexture(imgX);
  72. SDL_DestroyTexture(imgO);
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement