Second instance of C++ class not working

Button.cpp:


#include "vex.h"
#include "button.h"
#include "utils.h"

typedef struct {
  int x;
  int y;
  int w; 
  int h;
}
square;

typedef struct {
  int x;
  int y;
}
point;
typedef char *string;
square s;
bool clicked = false;

void (*functionpointer)(void);

bool checkCollsion(point p, square s)
{
    if(p.x >= s.x && p.x <= s.x + s.w && p.y >= s.y && p.y <= s.y + s.h){
      return true;
    }
    return false;
}

int checkCollisions()
{
  while(true)
  {
    waitUntil(Brain.Screen.pressing()); // Waits until the screen is being pressed
    do {
      point p = {Brain.Screen.xPosition(), Brain.Screen.yPosition()};
      if(checkCollsion(p, s))
      {
        functionpointer();
        
        return EXIT_SUCCESS;
      }
    }
    while(Brain.Screen.pressing());
  }
  return EXIT_SUCCESS;
}

void button::setup(int x, int y, int width, int height, string text, void (*ptr)(void))
{
    Brain.Screen.drawRectangle(x, y, width, height);
    s = {x, y, width, height};
    int tw = vexDisplayStringWidthGet(text);
    int th = vexDisplayStringHeightGet(text);
    int tl = s.x + (s.w / 2) - (tw/2);
    int tt = s.y + (s.h / 2) - (th / 2);
    Brain.Screen.printAt(tl, tt, false, text);
    functionpointer = ptr;
    task check(checkCollisions);
    return;
}

void button::setup(square s, string text, void (*ptr)(void)) {button::setup(s.x, s.y, s.w, s.h, text, ptr);}

main.cpp


#include "vex.h"
#include "button.h"
#include "utils.h"

using namespace vex;
typedef char *string;
button b, b2;
button::square x = {200, 0, 150, 50};

button::square y = {0, 0, 150, 50};

void Autonomous(void)
{
  Drivetrain.drive(forward);
}

void buttonFunction()
{
  Brain.Screen.clearScreen();
}
int main()
{
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  b.setup(x, "Drivercontrol?", buttonFunction);
  b2.setup(y, "Autonomous?", buttonFunction);

  Autonomous();
  return EXIT_SUCCESS;

}

ALSO yes I know I’m not doing the autonomous/drivercontrol stuff incorrectly that is for a reason.

Anyways the button I call setup first works but the second one does not. :confused:
I don’t know why???

1 Like

I have found the solution if you guys want I can post it but it’s a bit long.

Can you post it?

1 Like

main.cpp


#include "vex.h"
#include "button.h"
#include "utils.h"

using namespace vex;
typedef char *string;
button b, b2;
square x = {200, 0, 150, 50};

square y = {0, 0, 150, 50};

void Autonomous(void)
{
  Drivetrain.drive(forward);
}

void buttonFunction()
{
  Brain.Screen.print("clicked");
}
int CheckCollisions() {b.CheckCollisions(); return 0;}
int CheckCollisions2() {b2.CheckCollisions(); return 0;}

int main()
{
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  Brain.Screen.drawImageFromFile("logo.png", 365, 120);
  b.setup(x, "Drivercontrol?", buttonFunction);
  b2.setup(y, "Autonomous?", buttonFunction);
  task check(CheckCollisions);
  task check2(CheckCollisions2);

  Autonomous();
  while(true) {
    // Allow other tasks to run
    this_thread::sleep_for(10);
  }
  return EXIT_SUCCESS;

}

button.cpp


#include "vex.h"
#include "button.h"
#include "utils.h"

typedef char *string;
square s;
bool clicked = false;

void (*functionpointer)(void);

bool checkCollsion(point p, square s)
{
    return p.x >= s.x && p.x <= s.x + s.w && p.y >= s.y && p.y <= s.y + s.h;
}


  int button::CheckCollisions()   {
    while(true){
    waitUntil(Brain.Screen.pressing());
      point p = {Brain.Screen.xPosition(),Brain.Screen.yPosition()};
    if (checkCollsion(p, this->s)) {
        Brain.Screen.print(checkCollsion(p, this->s));

         this->functionpointer();
         return EXIT_SUCCESS;
    }
    }
  while(true) {
    // Allow other tasks to run
    this_thread::sleep_for(10);
  }
  return EXIT_SUCCESS;
}

void button::setup(int x, int y, int width, int height, string text, void (*ptr)(void))
{
    Brain.Screen.drawRectangle(x, y, width, height);
    s = {x, y, width, height};
    int tw = vexDisplayStringWidthGet(text);
    int th = vexDisplayStringHeightGet(text);
    int tl = s.x + (s.w / 2) - (tw/2);
    int tt = s.y + (s.h / 2) - (th / 2);
    Brain.Screen.printAt(tl, tt, false, text);
    functionpointer = ptr;
    return;
}

void button::setup(square s, string text, void (*ptr)(void)) {button::setup(s.x, s.y, s.w, s.h, text, ptr);}

button.h


    typedef struct {
      int x;
      int y;
      int w;
      int h;
    }
      square;
            
    typedef struct {
      int x;
      int y;
    }
    point;
  class button
  {
    public: 
      square s;
      void (*functionpointer)(void);
      int CheckCollisions();
      typedef char *string;
      void setup(int x, int y, int width, int height, string text, void (*ptr)(void));
      void setup(square s, string text, void (*ptr)(void));
  };

I’ll probably post my code on github later. :slight_smile:

1 Like

yea, super over complicated way of doing it.
see this.

or restructure so you only have one task checking all button instances.

6 Likes