I managed with some help to know how when two rectangles are intersecting each other, from there it should be easy to make what i just said in the title but ...
So, short story of what i just did below:
Created a for loop from 1 to Number_of_Obstacles
In that for an random obstacle (rectangle/square) is created and it will be checked if it is overlaped with all other obstacles created from 0 to the loop contor (or in other words every obstacle stored in the vector)
Again, the doOverLap function works. Tested it with a square which i made a controller and other random rectangle created on the screen. It outputs in chat when i'm overlaping it and trust me, i overlaped it from all angles.
Here is a picture with the overlaping issue: https://imgur.com/a/ZzorOcD
bool doOverlap(A a, B b)
{
if (a.x1 > b.x2 || b.x1 > a.x2)
return false;
if (a.y1 > b.y2 || b.y1 > a.y2)
return false;
return true;
}
struct Obstacles {
int X, Y;
void Create_Random_Obstacles(Obstacles Obj[], int Numar_Obstacole)
{
srand(time(NULL));
A Rectangle_1;
B Rectangle_2;
/* To avoid rendering outside of the screen */
int X_Axis = X_RESOLUTION - 40;
int Y_Axis = Y_RESOLUTION - 40;
int obstacolX = rand() % X_Axis + 1;
int obstacolY = rand() % Y_Axis + 1;
Obj[0].X = obstacolX;
Obj[0].Y = obstacolY;
for (int i = 1; i < Numar_Obstacole; i++)
{
obstacolX = rand() % X_Axis + 1;
obstacolY = rand() % Y_Axis + 1;
Rectangle_1.x1 = obstacolX;
Rectangle_1.x2 = obstacolX + 40;
Rectangle_1.y1 = obstacolY;
Rectangle_1.y2 = obstacolY + 40;
for (int j = 0; j < i; j++) {
Rectangle_2.x1 = Obj[j].X;
Rectangle_2.x2 = Obj[j].X + 40;
Rectangle_2.y1 = Obj[j].Y;
Rectangle_2.y2 = Obj[j].Y + 40;
if (doOverlap(Rectangle_1, Rectangle_2))
{
std::cout << "Overlap\n";
}
else
{
Obj[i].X = obstacolX;
Obj[i].Y = obstacolY;
}
}
}
}
void Render(SDL_Renderer* renderer, Obstacles Obj[], int Numar_Obstacole) {
for (int i = 0; i < Numar_Obstacole; i++)
{
SDL_Rect r{ Obj[i].X, Obj[i].Y, 40, 40 };
SDL_SetRenderDrawColor(renderer, 255, 160, 15, 255);
SDL_RenderFillRect(renderer, &r);
}
}
};
Restart selection when collision occurs, something like:
bool Has_Overlap(const Obstacles& obj, const Obstacles* Objs, int Size)
{
B Rectangle_2;
Rectangle_2.x1 = obs.X;
Rectangle_2.x2 = obs.X + 40;
Rectangle_2.y1 = obs.Y;
Rectangle_2.y2 = obs.Y + 40;
for (int i = 0; i != Size; ++i) {
A Rectangle_1;
Rectangle_1.x1 = Obs[i].X;
Rectangle_1.x2 = Obs[i].X + 40;
Rectangle_1.y1 = Obs[i].Y;
Rectangle_1.y2 = Obs[i].Y + 40;
if (doOverlap(Rectangle_1, Rectangle_2)) {
return true;
}
}
return false;
}
void Create_Random_Obstacles(Obstacles* Objs, int Size)
{
/* To avoid rendering outside of the screen */
const int X_Axis = X_RESOLUTION - 40;
const int Y_Axis = Y_RESOLUTION - 40;
for (int i = 0; i < Size; i++)
{
do {
Objs[i].X = rand() % X_Axis + 1;
Objs[i].Y = rand() % Y_Axis + 1;
} while (Has_Overlap(Objs[i], Objs, i));
}
}
I am trying to write a program that simulates a chess game with the FLTK library.
My problem is that I want to do two callbacks on an two dimension array of buttons, I want to click a button, then click another button and when the second button is clicked have the label of the first button switch to the label of the second button and then delete the label of the first button.
I feel like I need someway of storing the value of the the first button that is clicked, for example if I click FBoard[1][2] the i have a variable that is equal to FBoard[1][2] and open the second click replace the label of FBoard[1][2] to blank (assuming FBoard[1][2] is a cell on the board that has a piece on it. But I'm not sure how or even if this is the right approach.
Here is the cpp file:
#include"Window.h"
#include"ChessBoard.h"
const char * DisplayWindow::LastLabel;
bool DisplayWindow::flag;
DisplayWindow::DisplayWindow(int width, int height, const char*)
:Fl_Window(800, 650, "Chess"){
for (int X = 0; X <= 7; ++X){
for (int Y = 0; Y <= 7; ++Y){
// Leaves all positions that arent occupied by
// a figure at the start of the game blank
FBoard[X][Y] = new Fl_Button(10 + 50*Y, 100 + 50*X, 50, 50,"");
FBoard[X][Y]->callback((Fl_Callback*)DisplayWindow::ChangeButton);
}
}
flag = false;
MakeLabel();
LabelButton();
show();
}
DisplayWindow::~DisplayWindow(){}
void DisplayWindow::MakeLabel(){
for (int X = 0; X <= 7; ++X){
for (int Y = 0; Y <= 7; ++Y){
LBoard[X][Y] = (" ");
}
}
for (int X = 0; X <= 7; ++X){
LBoard[1][X] = ("WP");
// Occupies second row with white pawns
}
LBoard[0][0] = ("WR");
LBoard[0][1] = ("WH");
LBoard[0][2] = ("WB");
LBoard[0][3] = ("WQ");
LBoard[0][4] = ("WK");
LBoard[0][5] = ("WB");
LBoard[0][6] = ("WH");
LBoard[0][7] = ("WR");
for (int X = 0; X <= 7; ++X){
LBoard[6][X] = ("BP");
}
LBoard[7][0] = ("BR");
LBoard[7][1] = ("BH");
LBoard[7][2] = ("BB");
LBoard[7][3] = ("BQ");
LBoard[7][4] = ("BK");
LBoard[7][5] = ("BB");
LBoard[7][6] = ("BH");
LBoard[7][7] = ("BR");
}
void DisplayWindow::LabelButton(){
for (int X = 0; X <= 7; ++X){
FBoard[1][X]->label(LBoard[1][X]);
// Occupies second row with white pawns
}
FBoard[0][0]->label(LBoard[0][0]);
FBoard[0][1]->label(LBoard[0][1]);
FBoard[0][2]->label(LBoard[0][2]);
FBoard[0][3]->label(LBoard[0][3]);
FBoard[0][4]->label(LBoard[0][4]);
FBoard[0][5]->label(LBoard[0][5]);
FBoard[0][6]->label(LBoard[0][6]);
FBoard[0][7]->label(LBoard[0][7]);
for (int X = 0; X <= 7; ++X){
FBoard[6][X]->label(LBoard[6][X]);
}
FBoard[7][0] ->label(LBoard[7][0]);
FBoard[7][1] ->label(LBoard[7][1]);
FBoard[7][2] ->label(LBoard[7][2]);
FBoard[7][3] ->label(LBoard[7][3]);
FBoard[7][4] ->label(LBoard[7][4]);
FBoard[7][5] ->label(LBoard[7][5]);
FBoard[7][6] ->label(LBoard[7][6]);
FBoard[7][7] ->label(LBoard[7][7]);
for(int i=0 ; i<=7 ; i++)
{
for(int j=0 ; j<=7 ; j++)
{
int k=i+j;
if(k % 2 != 0 ){
FBoard[i][j]->color(FL_WHITE);
}
else if (k % 2 ==0 ){
FBoard[i][j]->color(FL_YELLOW);
}
}
}
}
void DisplayWindow::ChangeButton(Fl_Widget * o, void * v){
DisplayWindow* Win = (DisplayWindow *) v;
Fl_Button * NewBoard = (Fl_Button*) o;
if (Win->flag == false){
DisplayWindow::LastLabel = NewBoard->label();
NewBoard->label(" ");
Win->flag = true;
}
else{
NewBoard->label(DisplayWindow::LastLabel);
Win->flag = false;
}
}
And the Header file:
class DisplayWindow: public Fl_Window {
public:
DisplayWindow(int width, int height, const char* title=0);
virtual ~DisplayWindow();
void MakeLabel();
void LabelButton();
static void ChangeButton(Fl_Button * o, void * );
static bool flag;
bool flag1;
static const char * LastLabel;
private:
Fl_Button * FBoard[8][8];
char * LBoard[8][8];
};
#endif
cpp:
#include"Window.h"
#include"ChessBoard.h"
const char * DisplayWindow::LastLabel;
bool DisplayWindow::flag;
DisplayWindow::DisplayWindow(int width, int height, const char*)
:Fl_Window(800, 650, "Chess"){
for (int X = 0; X <= 7; ++X){
for (int Y = 0; Y <= 7; ++Y){
// Leaves all positions that arent occupied by
// a figure at the start of the game blank
FBoard[X][Y] = new Fl_Button(10 + 50*Y, 100 + 50*X, 50, 50,"");
FBoard[X][Y]->callback((Fl_Callback*)DisplayWindow::ChangeButton);
//FBoard[X][Y]->callback(ChangeButton);
}
}
flag = false;
MakeLabel();
LabelButton();
show();
}
DisplayWindow::~DisplayWindow(){}
void DisplayWindow::MakeLabel(){
for (int X = 0; X <= 7; ++X){
for (int Y = 0; Y <= 7; ++Y){
LBoard[X][Y] = (" ");
}
}
for (int X = 0; X <= 7; ++X){
LBoard[1][X] = ("WP");
// Occupies second row with white pawns
}
LBoard[0][0] = ("WR");
LBoard[0][1] = ("WH");
LBoard[0][2] = ("WB");
LBoard[0][3] = ("WQ");
LBoard[0][4] = ("WK");
LBoard[0][5] = ("WB");
LBoard[0][6] = ("WH");
LBoard[0][7] = ("WR");
for (int X = 0; X <= 7; ++X){
LBoard[6][X] = ("BP");
}
LBoard[7][0] = ("BR");
LBoard[7][1] = ("BH");
LBoard[7][2] = ("BB");
LBoard[7][3] = ("BQ");
LBoard[7][4] = ("BK");
LBoard[7][5] = ("BB");
LBoard[7][6] = ("BH");
LBoard[7][7] = ("BR");
}
void DisplayWindow::LabelButton(){
for (int X = 0; X <= 7; ++X){
FBoard[1][X]->label(LBoard[1][X]);
// Occupies second row with white pawns
}
FBoard[0][0]->label(LBoard[0][0]);
FBoard[0][1]->label(LBoard[0][1]);
FBoard[0][2]->label(LBoard[0][2]);
FBoard[0][3]->label(LBoard[0][3]);
FBoard[0][4]->label(LBoard[0][4]);
FBoard[0][5]->label(LBoard[0][5]);
FBoard[0][6]->label(LBoard[0][6]);
FBoard[0][7]->label(LBoard[0][7]);
for (int X = 0; X <= 7; ++X){
FBoard[6][X]->label(LBoard[6][X]);
}
FBoard[7][0] ->label(LBoard[7][0]);
FBoard[7][1] ->label(LBoard[7][1]);
FBoard[7][2] ->label(LBoard[7][2]);
FBoard[7][3] ->label(LBoard[7][3]);
FBoard[7][4] ->label(LBoard[7][4]);
FBoard[7][5] ->label(LBoard[7][5]);
FBoard[7][6] ->label(LBoard[7][6]);
FBoard[7][7] ->label(LBoard[7][7]);
for(int i=0 ; i<=7 ; i++)
{
for(int j=0 ; j<=7 ; j++)
{
int k=i+j;
if(k % 2 != 0 ){
FBoard[i][j]->color(FL_WHITE);
}
else if (k % 2 ==0 ){
FBoard[i][j]->color(FL_YELLOW);
}
}
}
}
void DisplayWindow::ChangeButton(Fl_Widget * o, void * v){
DisplayWindow* Win = (DisplayWindow *) v;
Fl_Button * NewBoard = (Fl_Button*) o;
if (Win->flag == false){
DisplayWindow::LastLabel = NewBoard->label();
NewBoard->label(" ");
Win->flag = true;
}
else{
NewBoard->label(DisplayWindow::LastLabel);
Win->flag = false;
}
}
h:
#ifndef WINDOW_H_
#define WINDOW_H_
#include<vector>
#include<limits>
#include<string>
#include<iostream>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
class DisplayWindow: public Fl_Window {
public:
DisplayWindow(int width, int height, const char* title=0);
virtual ~DisplayWindow();
void MakeLabel();
void LabelButton();
static void ChangeButton(Fl_Widget * o, void * );
static bool flag;
bool flag1;
static const char * LastLabel;
private:
Fl_Button * FBoard[8][8];
char * LBoard[8][8];
};
#endif
So I'm trying to make a Tetris game and I've come across something odd that I'm unsure about.
I have an array called bottom which stores the value of the lowest block - so, if there is no block in the first column, "bottom" will be 20.
If there's a square block occupying that first column, bottom would be 18. The weird thing is, when I set a breakpoint in my code to try to view the values for bottom, it says there is only one value in the array. In addition, my board, which is a 25 by 10 array, has the same problem, it only displays one dimension.
It seems the problem has to do with some kind of pointer issue, because it says (int (*)[10]) and (int *), where I think it should be a (int [25][10]) and (int [10]). I tried looking up array pointers and references, but the main thing I found was how to make an array of pointers and I'm not really quite sure how to word my searches.
If someone might know what's going wrong please let me know!
main.cpp
#include <chrono>
#include "makeboard.h"
int main() {
//declares and defines board
int board[24][10];
for (int y = 0; y < 24; y++) {
for (int x = 0; x < 10; x++) {
board[y][x] = 0;
}
}
makeboard(board);
}
tiles.h
#ifndef tiles_h
#define tiles_h
class O {
int board[24][10];
int x, y;
public:
void set_O (int[24][10], int, int);
};
void O::set_O (int board[24][10], int y, int x) {
board[y][x] = 1;
board[y][x+1] = 1;
board[y-1][x] = 1;
board[y-1][x+1] = 1;
}
class I {
int board[24][10];
int x, y, d;
public:
void set_I (int[24][10], int, int, int);
};
void I::set_I (int board[24][10], int d, int y, int x) {
if (d == 1 || d == 3) {
board[y-3][x] = 1;
board[y-2][x] = 1;
board[y-1][x] = 1;
board[y][x] = 1;
}
if (d == 2 || d == 4) {
board[y][x-1] = 1;
board[y][x] = 1;
board[y][x+1] = 1;
board[y][x+2] = 1;
}
}
class S {
int board[24][10];
int x, y, d;
public:
void set_S (int[24][10], int, int, int);
};
void S::set_S (int board[24][10], int d, int y, int x) {
if (d == 1 || d == 3) {
board[y-1][x] = 1;
board[y-1][x+1] = 1;
board[y][x] = 1;
board[y][x-1] = 1;
}
if (d == 2 || d == 4) {
board[y-2][x] = 1;
board[y-1][x] = 1;
board[y-1][x+1] = 1;
board[y][x+1] = 1;
}
}
class Z {
int board[24][10];
int x, y, d;
public:
void set_Z (int[24][10], int, int, int);
};
void Z::set_Z (int board[24][10], int d, int y, int x) {
if (d == 1 || d == 3) {
board[y][x] = 1;
board[y][x-1] = 1;
board[y+1][x] = 1;
board[y+1][x+1] = 1;
}
if (d == 2 || d == 4) {
board[y-1][x+1] = 1;
board[y][x+1] = 1;
board[y][x] = 1;
board[y+1][x] = 1;
}
}
class T {
int board[24][10];
int d, x, y;
public:
void set_T (int[24][10], int, int, int);
};
void T::set_T (int board[24][10], int d, int y, int x) {
if (d == 1 && (board[y+1][x-1] != 1 || board[y+1][x] != 1 || board[y+1][x+1] != 1)) {
board[y-1][x] = 1;
board[y][x-1] = 1;
board[y][x] = 1;
board[y][x+1] = 1;
}
if (d == 2 && (board[y+2][x] != 1 || board[y+1][x+1] != 1)) {
board[y-1][x] = 1;
board[y][x] = 1;
board[y][x+1] = 1;
board[y+1][x] = 1;
}
if (d == 3 && (board[y+1][x-1] != 1 || board[y+2][x] != 1 || board[y+1][x+1] != 1)) {
board[y][x-1] = 1;
board[y][x] = 1;
board[y][x+1] = 1;
board[y+1][x] = 1;
}
if (d == 4 && (board[y+1][x-1] != 1 || board[y+2][x] != 1)) {
board[y-1][x] = 1;
board[y][x-1] = 1;
board[y][x] = 1;
board[y+1][x] = 1;
}
}
class J {
int board[24][10];
int d, x, y;
public:
void set_J (int[24][10], int, int, int);
};
void J::set_J (int board[24][10], int d, int y, int x) {
if (d == 1) {
board[y-1][x-1] = 1;
board[y-1][x] = 1;
board[y-1][x+1] = 1;
board[y][x+1] = 1;
}
if (d == 2) {
board[y-2][x] = 1;
board[y-1][x] = 1;
board[y][x] = 1;
board[y][x-1] = 1;
}
if (d == 3) {
board[y][x-1] = 1;
board[y][x] = 1;
board[y][x+1] = 1;
board[y-1][x-1] = 1;
}
if (d == 4) {
board[y-2][x] = 1;
board[y-2][x+1] = 1;
board[y-1][x] = 1;
board[y][x] = 1;
}
}
class L {
int board[24][10];
int d, x, y;
public:
void set_L (int[24][10], int, int, int);
};
void L::set_L (int board[24][10], int d, int y, int x) {
if (d == 1) {
board[y-1][x-1] = 1;
board[y-1][x] = 1;
board[y-1][x+1] = 1;
board[y][x-1] = 1;
}
if (d == 2) {
board[y-2][x] = 1;
board[y-1][x] = 1;
board[y][x] = 1;
board[y][x-1] = 1;
}
if (d == 3) {
board[y-1][x-1] = 1;
board[y-1][x] = 1;
board[y-1][x+1] = 1;
board[y][x+1] = 1;
}
if (d == 4) {
board[y-2][x] = 1;
board[y-1][x] = 1;
board[y][x] = 1;
board[y][x+1] = 1;
}
}
#endif
makeboard.cpp
#include <iostream>
#include <limits>
#include <thread>
#include "makeboard.h"
#include "clearscreen.h"
#include "isBottom.h"
#include "isPressed.h"
#include "tiles.h"
using namespace std;
string icon[3] = { " ", " o ", " o " };
void makeboard(int board[24][10]) {
time_t srand( time(NULL) );
int block = srand % 7 ;
block = 3;
//declares pieces
O o;
I i;
S s;
Z z;
T t;
J j;
L l;
//declares and defines initial bottom
int bottom[10];
for (int i = 0; i < 10; i++) bottom[i] = 23;
//declares and defines initial block position
int y = 3;
int x = 4;
int d = 1;
while (!isBottom(board, block, y, x, d, bottom)) {
if (isPressed(0) && x > 0) {
x--;
}
if (isPressed(2) && x < 10) {
x++;
}
if (isPressed(1)) {
d += 1;
if (d == 4) {
d = 1;
}
}
//moves tile down
y++;
//clears screen
clearscreen();
//clears non set pieces
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 10; j++) {
if (board[i][j] == 1) {
board[i][j] = 0;
}
}
}
//adds blocks to board
switch (block) {
case 1:
o.set_O(board, y, x);
break;
case 2:
i.set_I(board, d, y, x);
break;
case 3:
s.set_S(board, d, y, x);
break;
case 4:
z.set_Z(board, d, y, x);
break;
case 5:
t.set_T(board, d, y, x);
break;
case 6:
j.set_J(board, d, y, x);
break;
case 7:
l.set_L(board, d, y, x);
break;
}
//builds board
cout << "╔══════════════════════════════╗" << endl;
for (int i = 4; i < 24; i++) {
cout << "║";
for (int j = 0; j < 10; j++) {
cout << icon[board[i][j]] ;
}
cout << "║" << endl;
}
cout << "╚══════════════════════════════╝" << endl;
cout << " 0 1 2 3 4 5 6 7 8 9 " << endl;
//resets initial tile position
if (isBottom(board, block, y, x, d, bottom)) {
y = 2;
//block = srand % 7;
}
//ends game
if (isBottom(board, block, 3, x, d, bottom)) {
cout << "You lose!";
return;
}
//delay
this_thread::sleep_for (chrono::milliseconds(100));
}
return;
}
clearscreen.cpp
#include <unistd.h>
#include <term.h>
#include <stdlib.h>
#include "clearscreen.h"
void clearscreen()
{
if (!cur_term)
{
void *a;
int result;
setupterm( NULL, STDOUT_FILENO, &result );
a = malloc(sizeof(int) *result);
free (a);
if (result <= 0) free (a); return;
}
putp( tigetstr( "clear" ) );
}
isBottom.cpp
#include "isBottom.h"
bool isBottom(int board[24][10], int block, int y, int x, int d, int bottom[10]) {
switch (block) {
case 1:
if (y == bottom[x] || y == bottom[x+1]) {
board[y][x] = 2;
board[y][x+1] = 2;
board[y-1][x] = 2;
board[y-1][x+1] = 2;
bottom[x] -= 2;
bottom[x+1] -= 2;
return true;
}
return false;
break;
case 2:
if (d == 1 || d == 3) {
if (y == bottom[x]) {
board[y-3][x] = 2;
board[y-2][x] = 2;
board[y-1][x] = 2;
board[y][x] = 2;
bottom[x] -= 4;
return true;
}
return false;
break;
}
if (d == 2 || d == 4) {
if (y == bottom[x-1] || y == bottom[x] || y == bottom[x+1] || y == bottom[x+2]) {
board[y][x-1] = 2;
board[y][x] = 2;
board[y][x+1] = 2;
board[y][x+2] = 2;
bottom[x-1]--;
bottom[x]--;
bottom[x+1]--;
bottom[x+2]--;
return true;
}
return false;
break;
}
case 3:
if (d == 1 || d == 3) {
if (y == bottom[x-1] || y == bottom[x] || y == bottom[x+1]) {
board[y-1][x] = 2;
board[y-1][x+1] = 2;
board[y][x] = 2;
board[y][x-1] = 2;
bottom[x-1] = 23 - y;
bottom[x] -= 2;
bottom[x+1] -= 2;
return true;
break;
}
return false;
break;
}
if (d == 2 || d == 4) {
if (y == bottom[x-1] || y == bottom[x]) {
board[y-2][x] = 2;
board[y-1][x] = 2;
board[y-1][x+1] = 2;
board[y][x+1] = 2;
bottom[x-1]--;
bottom[x] -= 1;
return true;
break;
}
return false;
break;
}
/*
case 3:
s.set_S(board, d, y, x);
break;
case 4:
z.set_Z(board, d, y, x);
break;
case 5:
t.set_T(board, d, y, x);
break;
case 6:
j.set_J(board, d, y, x);
break;
case 7:
l.set_L(board, d, y, x);
break;
*/
}
return true;
}
isPressed.cpp
#include <Carbon/Carbon.h>
#include "isPressed.h"
bool isPressed( unsigned short inKeyCode )
{
unsigned char keyMap[16];
GetKeys((BigEndianUInt32*) &keyMap);
return (0 != ((keyMap[ inKeyCode >> 3] >> (inKeyCode & 7)) & 1));
}
It depends on the scope of your array. For example:
int GetBottom(int* bottom);
int GetBottom2(const int (&bottom)[20]);
int main()
{
int localArray1d[20] = {};
int localArray2d[10][25] = {};
// putting a breakpoint here will allow you to see the full dimensions of the array because this function KNOWS what the object is (e.g. a 1d and 2d array respectively)
int lastBrick = GetBottom(localArray1d);
// When the array is passed to GetBottom, it's passed just as a pointer. Although it IS an array, the function GetBottom doesn't know that. We could just as simply pass it a single int*
int n = 0;
GetBottom(&n); // here we are only passing a single int pointer. GetBottom has no idea that your object is an array, it only knows it has an int*
lastBrick = GetBottom2(localArray1d);
// GetBottom2 only takes an array of 20 elements, so inspecting the object in that function allows you to see all the elements.
return 0;
}
int GetBottom(int* bottom)
{
// Having a breakpoint here will not allow you to see all the elements in an array since this function doesn't even know bottom IS an array.
}
int GetBottom2(const int (&bottom)[20])
{
// A breakpoint here will allow you to fully inspect bottom.
}
It's a little tricky when you refer to arrays the way you do, but an array like int array[5] degrades to int* array when you branch outside the scope in which it is defined. It's because arrays are r-values and need to degrade into a reference or pointer l-value (which lacks that info about how many elements there are) to pass them around. The gotcha part here is that you can still write a function which accepts int parameter[5] and the compiler will accept it, but will silently treat it like int* parameter. The same goes for the debugger.
So depending on your debugger, there's different ways to look at all the elements through a pointer anyway. For example, with this code:
int* ptr = some_array;
... in MSVC, I can only see the first element pointed to by ptr in the watch window. However, if I know that some_array has 10 elements, I can type ptr,10 in the watch window and it'll show me all 10 elements.
Also, again this is debugger-specific, but some debuggers are conveniently programmed to show the contents of standard containers no matter what in a beautifully-readable format. So if you can use contaners like std::vector, it'll make your debugging life easier if you're using such a debugger.
I have multiple SDL_Rects creating a snake that is supposed to stay in a specific area. Sometimes when the "snake" reaches the boundaries a part disappears.
void Snake::update(SDL_Surface *screen, int level)
{
old_pos.first = snake_rect[0];
if(x_axis)
snake_rect[0].x += snake_speed * level * direction_multiplier;
else if(!x_axis)
snake_rect[0].y += snake_speed * level * direction_multiplier;
for(unsigned int i = 1; i < snake_rect.size(); ++i)
{
old_pos.second = snake_rect[i];
snake_rect[i] = old_pos.first;
old_pos.first = old_pos.second;
}
boundariesCheck(screen);
/// Making the enemy move randomly
if(rand() % 100 < 10)
{
if(x_axis)
{
x_axis = false;
direction.second = rand() % 2;
if(direction.second)
direction_multiplier = 1;
else if(!direction.second)
direction_multiplier = -1;
}
else if(!x_axis)
{
x_axis = true;
direction.first = rand() % 2;
if(direction.first)
direction_multiplier = 1;
else if(!direction.first)
direction_multiplier = -1;
}
}
}
void Snake::draw(SDL_Surface *screen)
{
for(unsigned int i = 1; i < snake_rect.size(); ++i)
{
SDL_FillRect(screen, &snake_rect[i], 0xFF0000);
}
SDL_FillRect(screen, &snake_rect[0], 0xFF5500);
}
i am getting an error: invalid lvalue in assignment.
this is the only error with my program, it looks like a fatal compile time error regards on specially pthread.
i am trying to get the inputs in the runtime, using command line arguments, that's why i am getting an error, but previously i didn't get any error, when i run the program in static input initialized in the program itself.
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <sched.h>
#include <sys/types.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <stdint.h>
#define num_threads 8
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
unsigned int width = 1500;
unsigned int height = 1500;
unsigned int max_iterations = 30000;
unsigned int **color = NULL;
double min_re;
double max_re;
double min_im;
double max_im;
double x_factor;
double y_factor;
unsigned int NUM_OF_THREADS;
int chunk = 10;
int total_sum = 0;
bool file_write()
{
FILE *fractal = fopen("mandelbrot_imagepthread.ppm","w+");
if(fractal != NULL)
{
fprintf(fractal,"P6\n");
fprintf(fractal,"# %s\n", "Mandelbrot_imagepthread.ppm");
fprintf(fractal,"%d %d\n", height, width);
fprintf(fractal,"255\n");
int y = 0, x = 0;
unsigned int R = 0, G = 0, B = 0;
for(x = 0; x < width; ++x)
{
for(y = 0; y < height; ++y)
{
R = (color[y][x]*10)%255;
G = 255-((color[y][x]*10)%255);
B = ((color[y][x]*10)-150)%255;
if(R == 10) R = 11;
if(G == 10) G = 11;
if(B == 10) B = 11;
putc(R, fractal);
putc(G, fractal);
putc(B, fractal);
}
}
fclose(fractal);
}
return true;
}
int method(int x, int y, int max_iterations, double max_im,double min_re,double x_factor, double y_factor)
{
double c_im = max_im - y*y_factor;
double c_re = min_re + x*x_factor;
double Z_re = c_re, Z_im = c_im;
unsigned int col = 0;
for(unsigned n=0; n<max_iterations; ++n)
{
double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
if(Z_re2 + Z_im2 > 4)
{
col = n;
break;
}
Z_im = 2 * Z_re * Z_im + c_im;
Z_re = Z_re2 - Z_im2 + c_re;
}
return col;
}
void* method1(void* t)
{
double min_re = -2.0;
double max_re = 1.0;
double min_im = -1.2;
double max_im = min_im+(max_re-min_re)*height/width;
double x_factor = (max_re-min_re)/(width-1);
double y_factor = (max_im-min_im)/(height-1);
int x,y;
int sub_total = -1;
pthread_mutex_lock(&mut);
if(total_sum < height)
{
sub_total = total_sum;
total_sum = total_sum + chunk;
}
pthread_mutex_unlock(&mut);
while(sub_total > -1)
{
int start_point = sub_total;
int end_point = start_point + chunk;
for(y=start_point; y<end_point; y++)
{
for(x=0; x<width; ++x)
{
int m1;
uintptr_t m2;
m2 = (uintptr_t)t;
m1 = method(x,y,max_iterations,max_im,min_re,x_factor,y_factor);
if(m1)
{
color[x][y] = m1*40;
}
}
}
sub_total = -1;
pthread_mutex_lock(&mut);
if(total_sum < height)
{
sub_total = total_sum;
total_sum = total_sum + chunk;
}
pthread_mutex_unlock(&mut);
}
pthread_exit((void*)&t);
}
int main(int argc, char *argv[])
{
if(argc != 9)
{
printf("There is an error in the input given.\n");
return 0;
}
else
{
height = atoi(argv[1]);
width = atoi(argv[2]);
max_iterations = atoi(argv[3]);
min_re = atof(argv[4]);
max_re = atof(argv[5]);
min_im = atof(argv[6]);
max_im = atof(argv[7]);
num_threads = atoi(argv[8]);
}
color = (unsigned int**)malloc(height*sizeof(unsigned int*));
x_factor = (max_re-min_re)/(width-1);
y_factor = (max_im-min_im)/(height-1);
printf("height = %d\twidth = %d\tmaximum_iterations = %d\tminimum_x-value = %.2f\tmaximum_x-value = %.2f\tminimum_y-value = %.2f\tmaximum_y-value = %.2f\tno. of threads = %d\t\n",height,width,max_iterations,min_re,max_re,min_im,max_im,num_threads);
int x;
for(x = 0; x < height; x++)
{
color[x] = (unsigned int*)malloc(width*sizeof(unsigned int));
}
time_t ts,te;
time(&ts);
pthread_t t1[num_threads];
pthread_attr_t attr;
int l1;
void *att;
double value = 0.0;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
for(int i=0;i<num_threads;i++)
{
l1 = pthread_create(&t1[i], &attr, method1, (void *) i);
if(l1)
{
printf("There is some kind of error in thread creation: %d", l1);
exit(-1);
}
}
pthread_attr_destroy(&attr);
for(int i=0;i<num_threads;i++)
{
l1 = pthread_join(t1[i],&att);
if(l1)
{
printf("There is some kind of error in thread creation: %d", l1);
exit(-1);
}
double result = *(double *)att;
value += result;
}
time(&te);
double diff = difftime(te,ts);
file_write();
printf("Total Time elapsed: %.2f seconds\n",diff);
for(x = 0; x < height; x++)
{
free(color[x]);
}
free(color);
return 0;
pthread_exit(NULL);
}
The error here is that you define num_threads to be 8 with a #define directive instead declaring it as int!
Change #define num_threads 8 to int num_threads=8;
In general you should avoid #define directives because they are evil.
If you want to have a global constant variables declare it as static const rather than a #define. Those directives are substituted by the preprocessor to the following code and lead to the following (non-sense) code.
8 = atoi(argv[8])