I created this easy function and it seems there is something wrong with it that I really don't see what it is...
Evnt.motion.yrel output crazy number.
void EnthropyGenerator::OpenWindow()
{
SDL_Window *EnthropyGeneratorWindow;
SDL_Renderer* Renderer;
SDL_Init(SDL_INIT_VIDEO);
EnthropyGeneratorWindow =
SDL_CreateWindow("Enthropy Generator",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WindowSizeX,
WindowSizeY,
SDL_WINDOW_SHOWN |
SDL_WINDOW_BORDERLESS |
SDL_WINDOW_INPUT_GRABBED);
if (EnthropyGeneratorWindow == NULL)
{
ServerEngine::FatalError("Could not create window: " +
(std::string)SDL_GetError());
}
Renderer = SDL_CreateRenderer(EnthropyGeneratorWindow, -1, 0);
SDL_SetRenderDrawColor(Renderer, 10, 255, 0, 255);
SDL_RenderClear(Renderer);
SDL_RenderPresent(Renderer);
bool NeedMoreEntropy = true;
SDL_Event Evnt;
while (NeedMoreEntropy)
{
while (SDL_PollEvent(&Evnt))
{
if (Evnt.type == SDL_MOUSEMOTION)
{
std::cout << Evnt.motion.xrel << " and "
<< Evnt.motion.yrel << std::endl;
std::cout << m_EnthropyNeed << std::endl;
UpdateMousePosition(Evnt.motion.xrel, Evnt.motion.yrel);
AddEnthropy(m_MouseX, m_MouseY);
if (m_EnthropyNeed == 256)
{
NeedMoreEntropy = false;
}
}
}
}
SDL_DestroyWindow(EnthropyGeneratorWindow);
SDL_Quit();
}
void EnthropyGenerator::UpdateMousePosition(int deltaX, int deltaY)
{
m_MouseX += deltaX;
m_MouseY += deltaY;
}
void EnthropyGenerator::AddEnthropy(int deltaX, int deltaY)
{
m_EnthropyNeed++;
}
The output in the console is: 0 and 1985359926 and so on.
The output
It seems like something is not initialized or looks like a bad pointer. How can I fix this problem?
Related
I recently tried to implement OneLoneCoder's simple 3d Graphics Engine, from his Code-It-Yourself series, on SDL2. So I made some triangles that move on the window, but these triangles would appear to move at a slower framerate when I left my mouse completely still, and then move at their normal framerate if I started to move my mouse.
You see, to animate these triangles I used some while loops, something like this:
while (true) {
// Black Background
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
// Custom function that draws a triangle using SDL_RenderDrawLine()
drawTriangle(renderer, triangle);
SDL_RenderPresent(renderer); // updates the pixels on the renderer
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
goto Exit;
}
}
Pretty standard, right?
Here is an example of some code that has this exact same issue:
#include<iostream>
#include"SDL.h"
struct vec {
float x, y;
vec ( float x, float y)
: x(x), y(y)
{}
vec () {}
};
struct triangle {
vec p[3];
triangle() {}
};
void drawTriangle(SDL_Renderer* r, const triangle& tri) {
SDL_RenderDrawLine(r, tri.p[0].x, tri.p[0].y, tri.p[1].x, tri.p[1].y);
SDL_RenderDrawLine(r, tri.p[1].x, tri.p[1].y, tri.p[2].x, tri.p[2].y);
SDL_RenderDrawLine(r, tri.p[2].x, tri.p[2].y, tri.p[0].x, tri.p[0].y);
}
int main(int argc, char **argv) {
uint16_t width = 400;
uint16_t height = 300;
if (SDL_Init(SDL_INIT_VIDEO) > 0) { std::cout << "SDL_INIT FAILED\nERROR: " << SDL_GetError() << std::endl; }
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Event event;
window = SDL_CreateWindow("SDL Test :)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
if (window == NULL) { std::cout << "SDL_WINDOW FAILED\nERROR: " << SDL_GetError() << std::endl; }
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
triangle myTri;
myTri.p[0] = vec( width * 0.5, 0);
myTri.p[1] = vec( width * 0.333, height * 0.2);
myTri.p[2] = vec( width * 0.666, height * 0.2);
while (true) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
myTri.p[0].y += 0.01f;
myTri.p[1].y += 0.01f;
myTri.p[2].y += 0.01f;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
drawTriangle(renderer, myTri);
SDL_RenderPresent(renderer);
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
goto Exit;
}
}
Exit:
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I'm making a little game as a small project but I can't get an if statement to do anything. If I make it !statement it works though. I run this if statement to find which cube on the "grid" (An array or cubes I render in a for loop I didn't show) the mouse clicked on. I use C++ and SDL2 on a Mac. This is my code:
#include <iostream>
#include <SDL2/SDL.h>
void RenderRects(SDL_Renderer *renderer);
void ToggleRect(int MouseX, int MouseY);
struct Grid
{
bool IsActive;
SDL_Rect Rect;
};
Grid grid[228960];
int main()
{
bool IsRunning = true;
bool IsRunningSim;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 780, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
int h, w;
SDL_MaximizeWindow(window);
SDL_GetRendererOutputSize(renderer, &w, &h);
while (IsRunning)
{
// std::cout << w << std::endl;
// std::cout << h << std::endl;
SDL_Event ev;
while (SDL_PollEvent(&ev))
{
if (ev.type == SDL_QUIT)
{
IsRunning = false;
}
if (ev.type == SDL_MOUSEBUTTONDOWN)
{
int x, y;
SDL_GetMouseState(&x, &y);
ToggleRect(x, y);
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
//rendering
RenderRects(renderer);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
void RenderRects(SDL_Renderer *renderer)
{
for (int i = 0; i < 1440; i += 10)
{
for (int j = 0; j < 795; j += 10)
{
SDL_Rect Rect = {i, j, 10, 10};
grid[i].Rect = Rect;
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 225);
SDL_RenderDrawRect(renderer, &grid[i].Rect);
}
}
}
void ToggleRect(int MouseX, int MouseY)
{
SDL_Point MousePos;
MousePos.x = MouseX;
MousePos.y = MouseY;
for (int i = 0; i < 228961; i++)
{
if (SDL_PointInRect(&MousePos, &grid[i].Rect)) //This is the if that doesn't work.
{
std::cout << i << std::endl;
}
}
}
I have fixed this. I had to change my method of drawing since it was drawing over the rect and then showing after I changed its color. There was also an issue with generating the Rects that was probably effect it.
I'm creating a window and drawing a box but for some reason instead of drawing a box, the screen is just changed to that color. I have attached a photo of how the window looks and I will attach the source code.
#include <iostream>
#include <SDL.h>
#undef main
using namespace std;
int SCREEN_WIDTH = 650;
int SCREEN_HEIGHT = 650;
int main() {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Window *window = SDL_CreateWindow("Cells", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr) {
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr) {
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
SDL_RenderClear(renderer);
// renderTextures
SDL_Rect fillRect = { 122, 122, 122, 122};
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &fillRect);
SDL_RenderPresent(renderer);
}
return 0;
}
Doesn't Draw Correctly
SDL_RenderClear uses current draw colour, which you modified, so your clear and rectangle colour is the same. Set different clear colour (the one you want at background where nothing else is drawn) with e.g.
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// now draw your rectangles with different col
I was watching this series = https://www.youtube.com/watch?v=2NVgHrOFneg
and for some reason for the guy in the video the code works but for me it compiles fine but doesn't load an image. I really don't know what to do.
#include "SDL.h"
#include <iostream>
#include "SDL_image.h"
SDL_Texture *LoadTexture(std::string filePath, SDL_Renderer *renderTarget) //texture optimization function
{
SDL_Texture *texture = nullptr;
SDL_Surface *surface = IMG_Load(filePath.c_str());
if (surface == NULL)
std::cout << "Error 1" << std::endl;
else
{
texture = SDL_CreateTextureFromSurface(renderTarget, surface);
if (texture == NULL)
std::cout << "Error 2" << std::endl;
}
SDL_FreeSurface(surface);
return texture;
}
int main(int, char *argv[])
{
const int FPS = 144;
int frameTime = 0;
SDL_Window *window = nullptr;
SDL_Texture *currentImage= nullptr;
SDL_Renderer *renderTarget = nullptr;
SDL_Rect playerRect;
int frameWidth, frameHeight;
int textureWidth, textureHeight;
SDL_Init(SDL_INIT_VIDEO );
int imgFlags = IMG_INIT_PNG | IMG_INIT_JPG;
if (!(IMG_Init(imgFlags) != imgFlags))
{
std::cout << "Error: " << IMG_GetError() << std::endl;
}
window = SDL_CreateWindow("SDL Pong", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 720, SDL_WINDOW_SHOWN);
renderTarget = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
currentImage = LoadTexture("Untitled.jpg", renderTarget);
SDL_QueryTexture(currentImage, NULL, NULL, &textureWidth, &textureHeight);
SDL_SetRenderDrawColor(renderTarget, 0xFF, 0, 0, 0xFF);
frameWidth = textureWidth / 3;
frameHeight = textureHeight / 4;
playerRect.x = playerRect.y = 0;
playerRect.y = frameWidth;
playerRect.h = frameHeight;
bool isRunning = true; //game loop
SDL_Event ev;
while (isRunning)
{
while (SDL_PollEvent(&ev) != 0)
{
if (ev.type == SDL_QUIT)
isRunning = false;
}
frameTime++;
if (FPS / frameTime == 4)
{
frameTime = 0;
playerRect.x += frameWidth;
if (playerRect.x >= textureWidth)
playerRect.x =0;
}
SDL_RenderClear(renderTarget);
SDL_RenderCopy(renderTarget, currentImage, &playerRect, NULL);
SDL_RenderPresent(renderTarget);
}
SDL_DestroyWindow(window);
SDL_DestroyTexture(currentImage);
SDL_DestroyRenderer(renderTarget);
window = nullptr;
renderTarget = nullptr;
currentImage = nullptr;
SDL_Quit();
return 0;
}
This is the error message: http://imgur.com/LHMdt5F
IMG_Init returns bitfield of formats that was initialised. If resulting bitfield doesn't contain every format that was requested in flags, something gone wrong.
if (!(IMG_Init(imgFlags) != imgFlags)) checks if there is no error. Then you're trying to get error message, but there were no errors. Remove negation operator.
When you create the .exe and run it from an IDE it often stores the executable in a ../bin/.. directory. If Untitled.jpg is in the same directory as your source files, it will not find it.
SDL_GetBasePath(); will return the base path to your files. Check it out docs for it.
The string from SDL_GetBasePath() + "Untitled.jpg" will find and open the file.
The code below is meant to show a picture of a rider in the top left corner of the SDL window. With this code the image occasionally does appear as intended, but most of the time when I run the program I just get a black window. Any idea why?
#include "Game.h"
#include <iostream>
bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullScreen)
{
int flags = 0;
if (fullScreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
//attempt to initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING)==0) {
std::cout << "SDL init success \n";
//init the window
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (m_pWindow !=0) // window init successful
{
std::cout << "Window creation successful\n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if(m_pRenderer !=0) // renderer init success
{
std::cout << "renderer creation successful\n";
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
}
else
{
std::cout << "renderer init failed!\n";
return false; // render init failed
}
}
else
{
std::cout << "Window init failed!\n";
return false; //window init failed
}
}
else
{
std::cout << "SDL init failed!\n";
return false; // SDL init failed
}
std::cout << "Init success\n";
m_bRunning = true; // everything init-d successfully, main game loop running
SDL_Surface* pTempSurface = SDL_LoadBMP("rider.bmp");
if(!pTempSurface)
{
std::cout << "Error, tempsurface is null";
}
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurface);
SDL_FreeSurface(pTempSurface);
SDL_QueryTexture(m_pTexture, NULL, NULL, &m_sourceRectangle.w, &m_sourceRectangle.h);
m_destinationRectangle.x = m_sourceRectangle.x = 0;
m_destinationRectangle.x = m_sourceRectangle.x = 0;
m_destinationRectangle.h = m_sourceRectangle.h;
m_destinationRectangle.w = m_sourceRectangle.w;
return true;
}
void Game::render()
{
SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color
SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceRectangle, &m_destinationRectangle);
SDL_RenderPresent(m_pRenderer);
}
void Game::handleEvents()
{
SDL_Event event;
if (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
m_bRunning = false;
break;
default:
break;
}
}
}
void Game::clean()
{
std::cout << "cleaning app\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}