// ******** This source code corresponds to tutorial 5 of the developer guide ********** // The font used for displaying text declare global MyFont font; // The X and Y position of a click declare global PosX int; declare global PosY int; // The pen for the grid declare global GridPen pen; // The O and X images declare global SymbolX image; declare global SymbolO image; // The grid array declare global GridStatus[9] int; int global FREESQUARE = 0; int global XSQUARE = 1; int global OSQUARE = 2; // ************************************************************************************************************ // Called once when the plugin is started. Do one time setup here. // ************************************************************************************************************ function Init() { createfont(MyFont, "Arial", 12); createpen(GridPen, 0, 2, RGB(255, 255, 255)); // Create the O and X symbols from the GIFs. Parameter 1 is the image variable // to load into, parameter 2 is the path to the image to load. createimage(SymbolX, "SSE\\TicTacImages\\tictac-x.gif"); createimage(SymbolO, "SSE\\TicTacImages\\tictac-o.gif"); // Initialize the grid array. The array has 9 elements on a zero based index, which means // elements 0 to 8 are the only valid indexes. for(int iGridElement = 0; iGridElement < 9; iGridElement = iGridElement + 1) { // Set each element to a free square GridStatus[iGridElement] = FREESQUARE; } } // ************************************************************************************************************ // The plugin must draw all output here ONLY. This is the only valid drawing function. // ************************************************************************************************************ function Render() { int iWidth = getpanelwidth(); int iHeight = getpanelheight(); // Set the number of pixels in from the left of the display panel the grid should be at int iGridTopX = 10; int iGridWidth = iWidth - iGridTopX - 10; // The grid should be 1/4 of the way from the top of the display panel, and take up half the // total height of the panel. int iGridTopY = iHeight / 4; int iGridHeight = iHeight / 2; selectpen(GridPen); // First draw the vertical lines at 1/3 and 2/3 of the grid width int iGridThirdW = iGridWidth / 3; drawline(iGridTopX + iGridThirdW, iGridTopY, iGridTopX + iGridThirdW, iGridTopY + iGridHeight); drawline(iGridTopX + (iGridThirdW * 2), iGridTopY, iGridTopX + (iGridThirdW * 2), iGridTopY + iGridHeight); // Next draw the horizontal lines at 1/3 and 2/3 of the grid height int iGridThirdH = iGridHeight / 3; drawline(iGridTopX, iGridTopY + iGridThirdH, iGridTopX + iGridWidth, iGridTopY + iGridThirdH); drawline(iGridTopX, iGridTopY + (iGridThirdH * 2), iGridTopX + iGridWidth, iGridTopY + (iGridThirdH * 2)); int iSymbolPosX = iGridTopX; int iSymbolPosY = iGridTopY; // Iterate through the grid array and draw any symbols we find. The iGridElement++ // statement is shorthand for "iGridElement = iGridElement + 1", ie: increase the // value of iGridElement by one. for(int iGridElement = 0; iGridElement < 9; iGridElement++) { // Is this grid element an X? If so, draw the X symbol if(GridStatus[iGridElement] == XSQUARE) drawimage(SymbolX, iSymbolPosX, iSymbolPosY); // Is this grid element an O? If so, draw the O symbol if(GridStatus[iGridElement] == OSQUARE) drawimage(SymbolO, iSymbolPosX, iSymbolPosY); // Note: This is not the best way to calculate where to draw the symbols! It's // the easiest to understand though, which is the purpose of this tutorial. // Update the positioning variables. If we're currently processing element 2 or 5, // the next symbol must be reset to the left hand side of the grid, on the next line. if(iGridElement == 2 or iGridElement == 5) { iSymbolPosX = iGridTopX; iSymbolPosY = iSymbolPosY + (iGridHeight / 3); } else iSymbolPosX = iSymbolPosX + iGridWidth / 3; } } // ************************************************************************************************************ // Called when left mouse button is released. Params give X,Y pos of cursor at release. // ************************************************************************************************************ function OnMouseLButtonUp(int iX, int iY) { // Store the click position for use in the Render handler PosX = iX; PosY = iY; // Tell Brass to redraw our panel immediately redraw(); }