Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define SDL_MAIN_HANDLED
- #include <iostream>
- #include <SDL.h>
- using namespace std;
- SDL_Texture *carregaTextura(const char *nome, SDL_Renderer *render){
- SDL_Surface *surface = SDL_LoadBMP(nome);
- SDL_Texture *textura = SDL_CreateTextureFromSurface(render, surface);
- SDL_FreeSurface(surface);
- return textura;
- }
- bool desenhaXO(short (&mat)[3][3], int jog){
- int mouseX, mouseY;
- SDL_GetMouseState(&mouseX, &mouseY);
- if(mat[(mouseY / 191)][(mouseX / 203)] == -1){
- mat[(mouseY / 191)][(mouseX / 203)] = jog;
- return true;
- }
- //cout << (mouseY / 575)*3 << ", " << (mouseX / 611)*3 << "\n";
- return false;
- }
- int main(){
- SDL_Window *mainWindow = SDL_CreateWindow("Jogo da Velha!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 611, 575, 0);
- SDL_Renderer *render = SDL_CreateRenderer(mainWindow, -1, SDL_RENDERER_ACCELERATED);
- SDL_Texture *tabuleiro = carregaTextura("img/tabuleiro.bmp", render);
- SDL_Texture *imgX = carregaTextura("img/X.bmp", render);
- SDL_Texture *imgO = carregaTextura("img/0.bmp", render);
- short mat[3][3] = {{-1, -1, -1},
- {-1, -1, -1},
- {-1, -1, -1}};
- int jogador = 0;
- bool end = false;
- while(!end){
- SDL_Event event;
- while(SDL_PollEvent(&event)){
- switch(event.type){
- case SDL_QUIT:
- end = true;
- break;
- case SDL_MOUSEBUTTONDOWN:
- desenhaXO(mat, jogador);
- jogador = (jogador + 1) % 2;
- for(int y = 0; y < 3; y++){
- for(int x = 0; x < 3; x++){
- cout << mat[y][x];
- }
- cout << "\n";
- }
- cout << "\n\n";
- break;
- }
- }
- SDL_RenderCopy(render, tabuleiro, nullptr, nullptr);
- for(int y = 0; y < 3; y++){
- for(int x = 0; x < 3; x++){
- SDL_Rect XOPos = {x * 203 + 50, y * 191 + 50, 71, 71};
- if(mat[y][x] == 1){
- SDL_RenderCopy(render, imgX, nullptr, &XOPos);
- } else if(mat[y][x] == 0){
- SDL_RenderCopy(render, imgO, nullptr, &XOPos);
- }
- }
- }
- SDL_RenderPresent(render);
- SDL_Delay(1000/60);
- }
- SDL_DestroyTexture(tabuleiro);
- SDL_DestroyTexture(imgX);
- SDL_DestroyTexture(imgO);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement