How Instantiate Objects

How I can create new object and save him on screen?
Code:
`#include

#include <SDL.h>

const char* MAINWINDOW_TITLE = “SANDBOX”;
int screenWidth = 1280;
int screenHeight = 720;
int mousePosX, mousePosY;

SDL_Window* window;
SDL_Renderer* renderer;

int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);

window = SDL_CreateWindow(MAINWINDOW_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenWidth, screenHeight, SDL_WINDOW_SHOWN);

renderer = SDL_CreateRenderer(window, -1, 0);

bool isRunning = true;

while (isRunning)
{	
	SDL_Event evnt;
	SDL_PollEvent(&evnt);

	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);

	SDL_GetMouseState(&mousePosX, &mousePosY);

	int solid_stone_num;

	// Objects
	SDL_Rect solid_stone;

	// Event Handler
	switch (evnt.type)
	{
	case SDL_QUIT:
		isRunning = false;
		break;
	case SDL_MOUSEBUTTONDOWN:
		if (evnt.button.button == SDL_BUTTON_LEFT)
		{
			solid_stone.x = mousePosX - 25;
			solid_stone.y = mousePosY - 25;
			solid_stone.w = 50;
			solid_stone.h = 50;

			std::cout << "solid_stone_1 created at (" << mousePosX << "," << mousePosY << ")" << std::endl;
		}
		break;
	}

	SDL_RenderClear(renderer);

	SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
	SDL_RenderFillRect(renderer, &solid_stone);

	SDL_RenderPresent(renderer);
}

SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);

SDL_Quit();

return 0x0;

}`

Hello and welcome to the forum!

Do you wish to create an SDL_Rect each time the mouse button is clicked, at the position of the mouse pointer?

If that’s the case you can always use a dynamic container, like std::vector, std:list etc, containing SDL_Rect objects. Then, each time the mouse pointer is clicked, add a new SDL_Rect to the container and then later, in the rendering stage, loop through the container and render each quad, if desirable.

1 Like

Thanks for the answer. Sorry, I’m a beginner in programming. How do I add SDL_Rect when I click the mouse button?

No worries, we have all been beginners. :slight_smile:

I can’t help with the code right now but if you haven’t gotten an answer from someone else when I get home, I will prepare a code example for you.

Do you want to be able to freely place a SDL_Rect where ever you want on the screen or do you want the SDL_Rect’s positions to be restricted to a grid-like pattern, like in a level editor?

1 Like

I making a Sandbox game and i need a freely place

I make this code and programm create and don’t save this line (not block 50x50):


My code:

	SDL_Rect solid_stone;

	// Event Handler
	switch (evnt.type)
	{
	case SDL_KEYDOWN:
		switch (evnt.key.keysym.sym)
		{
		case SDLK_x:

			break;
		case SDLK_DELETE:
		SDL_RenderClear(renderer);
		break;
		}
	case SDL_MOUSEBUTTONDOWN:
		if (evnt.button.button == SDL_BUTTON_LEFT)
		{
			solid_stone.x = mousePosX;
			solid_stone.y = mousePosY;
			solid_stone.w = 50;
			solid_stone.w = 50;

			std::list<SDL_Rect>solidStone = { solid_stone };

			solidStone.push_back(solid_stone);
			std::cout << "solid_stone created at (" << mousePosX << "," << mousePosY << ")" << std::endl;
		}

It looks like you have some knowledge about how to use the std::list container, that’s good.

The problem with your code at the moment is that you’re declaring your std::list object inside the SDL_MOUSEBUTTONDOWN switch case, which means the object will be destroyed when the function (the if-statement inside the switch case) goes out of scope.

Rather than declaring the std:list there, move it out of the function and place it outside of main instead, where you declare screenWidth, screenHeight, the SDL_Window* pointer etc.

okay, I’ll learn this moment. thanks

Okay, the object appears but is not saved. May be problem with std::list


Code:

#include <iostream>

#include <SDL.h>
#include <list>

const char* MAINWINDOW_TITLE = "SANDBOX";
int screenWidth = 1280;
int screenHeight = 720;
int mousePosX, mousePosY;

// Objects
SDL_Rect solid_stone;

// Main
std::list<SDL_Rect>solidStone = { solid_stone };

SDL_Window* window;
SDL_Renderer* renderer;

int main(int argc, char* argv[])
{
	SDL_Init(SDL_INIT_EVERYTHING);

	window = SDL_CreateWindow(MAINWINDOW_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenWidth, screenHeight, SDL_WINDOW_SHOWN);

	renderer = SDL_CreateRenderer(window, -1, 0);




	bool isRunning = true;

	while (isRunning)
	{	
		SDL_Event evnt;
		SDL_PollEvent(&evnt);

		SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);

		SDL_GetMouseState(&mousePosX, &mousePosY);

		// Event Handler
		switch (evnt.type)
		{
		case SDL_KEYDOWN:
			switch (evnt.key.keysym.sym)
			{
			case SDLK_x:

				break;
			case SDLK_DELETE:
			SDL_RenderClear(renderer);
			break;
			}
		case SDL_MOUSEBUTTONDOWN:
			if (evnt.button.button == SDL_BUTTON_LEFT)
			{
				solid_stone.x = mousePosX;
				solid_stone.y = mousePosY;
				solid_stone.w = 50;
				solid_stone.h = 50;

				solidStone.push_back(solid_stone);

				std::cout << "solid_stone created at (" << mousePosX << "," << mousePosY << ")" << std::endl;
			}
			break;
		case SDL_QUIT:
			isRunning = false;
			break;
		}
		SDL_RenderClear(renderer);
		SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
		SDL_RenderFillRect(renderer, &solid_stone);
		SDL_RenderPresent(renderer);
	}

	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);

	SDL_Quit();

	return 0x0;
}

You’re only rendering the SDL_Rect object solid_stone, which are declared above your list object.
Remove that SDL_Rect object and instead, whenever the mouse button is pressed, in the if-statement, create a local SDL_Rect there, set its data and then finally push it into the list.

Later, when it’s time to render the quads, loop through the list, retrieve each quad’s data and render each quad.

1 Like

I make this code:

case SDL_MOUSEBUTTONDOWN:
if (evnt.button.button == SDL_BUTTON_LEFT)
{
SDL_Rect solid_stone;
solid_stone.x = mousePosX - 50/2;
solid_stone.y = mousePosX - 50/2;
solid_stone.w = 50;
solid_stone.h = 50;

  		solidStone.push_back(solid_stone);
  	}
  	break;

Please tell me how i can retrieve data from std::list

std::list doesn’t have the index operator overloaded, which makes data retrieval a bit annoying. What this means is that you can’t index into it, for example by writing MyList[0].MyData to retrieve the data at index 0.

If you want to be able to index into it, you’ll have to change to another container type that you can use the index operator on, like std::vector for example.

In your case, you want to loop through the list and retrieve the SDL_Rect data. I have added a code example below, showing how to loop through it and to render each quad/rect.

void Render(void)
{
  SDL_RenderClear(pMyRenderer);
  SDL_SetRenderDrawColor(pMyRenderer, 255, 255, 255, 255);

  std::list<SDL_Rect>::const_iterator ItBeg = MyList.begin();
  std::list<SDL_Rect>::const_iterator ItEnd = MyList.end();

  for(; ItBeg != ItEnd; ++ItBeg)
  {
    SDL_RenderFillRect(pMyRenderer, &(*ItBeg));
  }

  SDL_RenderPresent(pMyRenderer);
}
1 Like

It’s working, but there is one problem. Objects spawning on one line

I solved this problem. I forgot rename mousePosX to mousePosY. Thank you for the help!

No worries. Good luck with your project! :slight_smile:

1 Like

Agreed! It is good to use container in that way. I had the similar issues for going out of scope now i learnt from my past experiences. Nowadays gaming experience is depend mainly with keeping such technicalities on account to feel good while laying. Moreover still gaming accessories are important like having a proper full featured gaming keyboard. And same with case of chair and mouse and specially powerful built in gaming processor.