I tried your way and it doesnt seem to do anything when moving source rectangle at least with the way I set up my code. Moving destination rect allows me to actually see the point but it starts drawing random points in a different color. I assume moving dest rect updates the position of the screen so 50,50 is now somewhere else and that is why I am getting these new random points?
Let me be more clear on what I am trying to achieve here. The user inputs a keyword command for my calculator program such as plotGUI(sin,-6,6,-1.1,1.1,0.075,0.09). This runs the PlotGUI.cpp (Which I have only put the SDL relative stuff in this post. My Vect2D gets its data from a function call before what you see below):
PlotGUI.cpp
Graphics graphics;
Camera camera;
SDL_Event event;
Input input;
Vect2D<std::pair<double, double> > points
SDL_Texture* texture = SDL_CreateTexture(graphics.getRenderer(),
SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 640, 480);
SDL_Rect destRect = {0,0,640,480};
SDL_Rect srcRect = { 0,0,640,480 };
while (true) {
input.beginNewFrame(); //reset released key/press key
if (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) {
if (event.key.repeat == 0) {
input.keyDownEvent(event); //if we are holding key : start keydown event
}
}
else if (event.type == SDL_KEYUP) { // if key was released
input.keyUpEvent(event);
}
else if (event.type == SDL_QUIT) {
throw 0; //User exits window (Force)
}
}
if (input.wasKeyPressed(SDL_SCANCODE_ESCAPE) == true) {
throw 0; //User exits normally with ESC key
}
else if (input.isKeyHeld(SDL_SCANCODE_A) == true) {
graphics.d_rect.x -= 10;
}
else if (input.isKeyHeld(SDL_SCANCODE_D) == true) {
graphics.d_rect.x += 10;
}
else if (input.isKeyHeld(SDL_SCANCODE_W) == true) {
graphics.d_rect.y -= 10;
}
else if (input.isKeyHeld(SDL_SCANCODE_S) == true) {
graphics.d_rect.y += 10;
}
SDL_SetRenderTarget(graphics.getRenderer(), texture);
SDL_SetRenderDrawColor(graphics.getRenderer(), 0x00, 0x00, 0x00, 0x00);
graphics.clear();
SDL_SetRenderDrawColor(graphics.getRenderer(), 0xFF, 0x00, 0x00, 0x00);
for (int i = 0; i < points.size(); i++) {
SDL_RenderDrawPoint(graphics.getRenderer(), points[i].first, points[i].second);
//std::cout << "x, y = " << points[i].first << "," << points[i].second << std::endl;
}
//SDL_RenderDrawPoint(graphics.getRenderer(), 50, 50);
SDL_SetRenderTarget(graphics.getRenderer(), NULL);
graphics.blitSurface(texture, NULL, &destRect);
//SDL_RenderCopy(graphics.getRenderer(), texture, NULL, NULL);
SDL_RenderPresent(graphics.getRenderer());
camera.Update(std::min(ELAPSED_TIME_MS, MAX_FRAME_TIME), graphics);
Where graphics.blitSurface() refers to (In Graphics.cpp)
Graphics.cpp
void Graphics::blitSurface(SDL_Texture* texture, SDL_Rect* sourceRectangle, SDL_Rect* destinationRectangle) {
SDL_Rect tmp = { destinationRectangle->x - Camera::GetRect().x, destinationRectangle->y - Camera::GetRect().y, destinationRectangle->w, destinationRectangle->h };
SDL_RenderCopy(this->_renderer, texture, sourceRectangle, &tmp);
}
And camera.Update() refers to (In camera.cpp)
Camera.cpp
SDL_Rect Camera::GetRect()
{
SDL_Rect GetRect = { m_rect.x,m_rect.y,640,480 };
return GetRect;
}
void Camera::Update(float elapsedTime, Graphics& graphics)
{
m_rect.x = graphics.d_rect.x;
m_rect.y = graphics.d_rect.y;
GetRect();
}
The calculations when using the plotGUI command are indeed correct and they do plot with a different command that plots in on the terminal window using symbols and not a graphical interface. So I know the math for the points is correct. Itâs just that using SDL to draw them does not work out for me so far.
I would like for the user to be able to plot their function and if it has a large amount of points for example 150 coordinates. It would be great if they can scroll around the screen to see it all. I am open to scaling and fitting everything in my 640x480 window (I donât mind making it bigger thatâs just a test resolution).
So what exactly am I doing wrong here?
As for @sjr
Iâve never used something like offsets. Can you explain in-depth what I would need to do?