1

I am very new to C++ and decided to start off by making tic tac toe with RayLib as a graphics engine. The code below sets up a screen, draws the grid and checks for an input. The part I was working on is showing an X or O in the fields that were clicked on. I finally got the program to draw text but it seems to draw the whole array instead of one letter.

#include "raylib.h"
#include <string.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
    //INITIALIZE//
    int screenWidth = 750;
    int screenHeight = 750;
    char matrix[9] = {'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'};
    char currentPlayer = 'X';
    InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
    SetTargetFPS(60);
    
    while (!WindowShouldClose())
    {   
        //INPUT//
        if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
            int mouseX = GetMouseX();
            int mouseY = GetMouseY();
            double x = floor(mouseX/250);
            double y = floor(mouseY/250);
            int index = x + 3*y;
            matrix[index] = currentPlayer;
            currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
        }
        //DRAWING//
        BeginDrawing();
        ClearBackground(WHITE);
        for (int i = 1; i < 3; i++) {
            int num = i*250;
            DrawLine(num, 0, num, screenWidth, LIGHTGRAY);
            DrawLine(0, num, screenHeight, num, LIGHTGRAY);
        }
        //Code I was working on
        for (int i = 0; i < 9; i++) {
            if (matrix[i] != 'E') {
                int textX = 115 + i*250 - (i%3)*750;
                int textY = 115 + (i%3)*250;
                char text = matrix[i];
                DrawText(&text, textX, textY, 20, LIGHTGRAY); //The problem is here
            }
        }
        EndDrawing();
    }
    CloseWindow();
    return 0;
}

When I click the top left cell to draw an X it draws 'XXEEEEEEEE0?D' instead. Does anyone know how to draw only one character from the array?

Thanks in advance!

  • Not knowing raylib I expect DrawText probably does not draw a single character if it did you would pass it text and not &text. – drescherjm Aug 17 '20 at 18:13
  • 1
    If `DrawText` takes `char*` then it assumes a C string. You're passing a single character, which is invalid. It **must** be NUL terminated. Try `char text[2] = {matrix[i], 0}`. – tadman Aug 17 '20 at 18:14

3 Answers3

3

C-style string is terminated with null character (\0), so you must add that or you have it read out-of-bounds and invoke undefine behavior.

Therefore,

char text = matrix[i];

should be

char text[] = {matrix[i], '\0'};

and

DrawText(&text, textX, textY, 20, LIGHTGRAY);

should be

DrawText(text, textX, textY, 20, LIGHTGRAY);

(remove & before text)

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
3

DrawText() expects a null-terminated string as input, but you are giving it a single char instead. Change this:

char text = matrix[i];
DrawText(&text, ...);

To this:

char text[2] = {matrix[i], '\0'};
DrawText(text, ...);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Which version of DrawText is this? Certainly not the one from here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawtext – SergeyA Aug 17 '20 at 18:19
  • 2
    @SergeyA No, it is not the one from the Win32 API. The OP said they are using the [RayLib](https://www.raylib.com) graphics library, which has its own [`DrawText()`](https://www.raylib.com/cheatsheet/cheatsheet.html) function in the `text` module: `void DrawText(const char *text, int posX, int posY, int fontSize, Color color);` – Remy Lebeau Aug 17 '20 at 18:26
0

You could do:

            char text = matrix[i];
            char shortenedText[2] = {0, 0};
            shortenedText[0] = text;
            DrawText(shortenedText, textX, textY, 20, LIGHTGRAY); //The problem is here

Basically building a very small string with just one char.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42