I am doing one of my first projects on SFML C++ and im trying to combine things now.
What im trying to do is having my Circle which i made with :
sf::CircleShape shape(50);
shape.setPosition(800, 450);
and
shape.setFillColor(sf::Color(100, 250, 50));
now i am trying to move it using W, A, S, D or arrow keys.
But i am not sure how to this, i tried several things like :
if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Up))
Sprite.Move(spriteSpeed * App.GetFrameTime(), 0);
But i am not sure what im doing wrong, can someone help me?
Thanks in advance!
This is the code i have atm.
#include "stdafx.h"
#include<SFML/Graphics.hpp>
#include<string>
#include<iostream>
int main()
{
//Here we declare the render window so we can talk to it.
sf::RenderWindow window;
//sf::VideoMode is to set the size of the window
//The seconds parameter (the string) is for setting the title
//The style is to show/hide the close button and the title bar, or to set full screen
window.create(sf::VideoMode(1600, 900), " My First SFML Game", sf::Style::Titlebar | sf::Style::Close | sf::Style::Resize);
//----------------------------------wait for a key to be pressed-------------------------------------
//This shows a message that you should press a key
/*std::cout << "Press a key to continue." << std::endl;*/
//---------------------------------------------------------------------------------------------------
//----------------------------------Showing a message------------------------------------------------
//Define the messages that will be showed, and the display text
std::string message = "Hello my name is Jean-Paul van Houten";
std::string display = "";
int index = 0;
window.setKeyRepeatEnabled(false);
//----------------------------------------------------------------------------------------------------
sf::CircleShape shape(50);
shape.setPosition(800, 450);
//this while loop will only be called if the window is open.
while(window.isOpen())
{
//Define the event variable
sf::Event eventSF;
//Check if there is an event
while(window.pollEvent(eventSF))
{
shape.setFillColor(sf::Color(100, 250, 50));
//shape.setPosition(eventSF.mouseMove.x 0 sha,eventSF.mouseMove.y);
window.clear();
switch(eventSF.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::MouseEntered:
std::cout << "Mouse within screen bounds" << std::endl;
break;
case sf::Event::MouseLeft:
std::cout << "Mouse outisde the screen bounds" << std::endl;
break;
case sf::Event::MouseMoved:
std::cout << "X: " << eventSF.mouseMove.y << " Y: " << eventSF.mouseMove.y << std::endl;
break;
case sf::Event::MouseButtonPressed:
if(eventSF.mouseButton.button == sf::Mouse::Left)
std::cout << "Left Button Pressed At: X: " << eventSF.mouseButton.x << " Y: " << eventSF.mouseButton.y << std::endl;
break;
case sf::Event::MouseWheelMoved:
std::cout << "Scrolled: " << eventSF.mouseWheel.delta << std::endl;
break;
case sf::Event::GainedFocus:
std::cout << "Window Active" << std::endl;
break;
case sf::Event::LostFocus:
std::cout << "Window Not Active" << std::endl;
break;
case sf::Event::Resized:
std::cout << "Width: " << eventSF.size.width << " Height: " << eventSF.size.height << std::endl;
break;
case sf::Event::TextEntered:
if(eventSF.text.unicode != 8)//(eventSF.text.unicode >= 33 && eventSF.text.unicode <= 126) //This is to only include the characters between the number, now we use punctuation and letters.
std::cout << (char)eventSF.text.unicode;
else if(eventSF.text.unicode == 8)
display = display.substr(0, display.length() - 1);
system("cls");
std::cout << display;
break;
}
window.draw(shape);
//If you release a key
if(eventSF.type == sf::Event::KeyReleased)
{
//and this key is the enter key
if(eventSF.key.code == sf::Keyboard::Return)
{
display += message[index];
index ++;
system("cls"); //CLS on windows, clear on mac/linux
std::cout << display;
}
}
}
window.display();
}
}
First, you have to add the KeyPressed event handling inside the event poll switch, and inside, the code to move your sprite
switch(eventSF.type)
{
[...]
case sf::Event::KeyPressed:
if(eventSF.key.code == sf::Keyboard::Up)
{
shape.move(0, 1)
}
break;
}
Also,
shape.setFillColor(sf::Color(100, 250, 50));
shouldn't be inside the game loop.
And this
window.clear();
window.draw(shape);
window.display();
should be outside the event poll loop:
while(window.isOpen())
{
//Define the event variable
sf::Event eventSF;
//Check if there is an event
while(window.pollEvent(eventSF))
{
[...]
}
window.clear();
window.draw(shape);
window.display();
}
Related
Trying to make buttons using SFML for Comp Sci final and really don't want to draw invisible sprites over every single button.
I found some solutions but they were all using older versions of sfml and those functions have since been removed or changed and not sure what they've been changed to.
while(window.isOpen()){
Event event;
while(window.pollEvent(event)){
switch(event.type)
{
case Event::Closed:
window.close();
cout << "Window Closed!" << endl;
break;
case Event::MouseButtonPressed:
if(event.mouseButton.button == Mouse::Left){
cout << " if(event.mouseButton.button == Mouse::Left){" << endl;
if(equationsButtonText.getLocalBounds().contains(event.mouseButton.x, event.mouseButton.y)){
cout << "This works!" << endl;
}
}
default:
break;
}
}
}
cout << " if(event.mouseButton.button == Mouse::Left){" << endl; was just to test how far into the loop it got.
getLocalBounds returns the bounds in the local coordinates of the text. You need to use getGlobalBounds to get it in world coordinates.
You also need to use the mapPixelToCoords method of your window to transform the coordinates of the mouse also to world coordinates.
It would be something like this:
if(equationsButtonText.getGlobalBounds().contains(window.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y}))){
cout << "This works!" << endl;
}
Stack Trace :
So, I'm trying to create a 2D game and right now I'm trying to display and move a image, however when I Debug the game/run it, visual studio and the game freezes and can't quit, not even when using the task manager to kill it.
The only way to unfreeze it is by logging off or restarting the pc which forces both of them to close.
I also get some weird error :
Unhandled exception at 0x71002A95 (SDL2_ttf.dll) in SDLGame.exe: 0xC0000005: Access violation reading location 0x00000000.
I have no idea what it means and how to fix it, but I'm guessing it has something to do with my code that I need to change.
Here's my code :
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
// Macros
#define pause system("PAUSE"); // Works on windows only, removed in alpha / beta versions.
// Pre "init" of functions
void QuitGame();
int InitGame();
void processInput();
void InitRects();
// Variables
int FramesPassed = 0;
int FramesPerSecond = 0; // Not used yet
SDL_Renderer* renderer = nullptr;
SDL_Window* window = nullptr;
SDL_Event evnt;
SDL_Rect sprite1_Rect;
SDL_Rect FPS_Text_Rect;
TTF_Font* Sans = TTF_OpenFont("Fonts/Aaargh.ttf", 40);
SDL_Color Color_White = { 255, 255, 255 };
SDL_Surface* FPS_Text_Surface = nullptr;
SDL_Texture* FPS_Text = nullptr;
SDL_Texture* testImg = nullptr;
static bool isRunning = true;
int SDL_main(int argc, char* argv[])
{
InitGame();
InitRects();
std::cout << "Displaying text on screen using SDL TTF doesn't work" << std::endl;
std::cout << "This happens when the TTF Surface is being rendered on screen" << std::endl;
std::cout << "check line : 123 and 124." << std::endl;
while (isRunning)
{
FramesPassed++;
processInput();
SDL_RenderClear(renderer); // Clears the last/current frame?
// Render testImage on screen. (needs to be between render present and clear.)
SDL_RenderCopy(renderer, testImg, NULL, &sprite1_Rect);
SDL_RenderCopy(renderer, FPS_Text, NULL, &FPS_Text_Rect);
SDL_RenderPresent(renderer); // Pretty much draws everything again.
}
return 0;
QuitGame();
}
int InitGame()
{
std::cout << "Game Initializing..." << std::endl;
std::cout << "TTF SDL Initializing..." << std::endl;
if (TTF_Init() < 0)
{
std::cout << "SDL TTF Failed To Initialize : " << TTF_GetError() << std::endl;
pause
QuitGame();
}
else
{
std::cout << "SDL TTF Initialized Successfully" << std::endl;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cout << "SDL Initialization Failed : " << SDL_GetError() << std::endl;
pause
QuitGame();
}
else
{
std::cout << "SDL Initializing" << std::endl;
window = SDL_CreateWindow("Game Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 720, SDL_WINDOW_SHOWN);
if (window == NULL)
{
std::cout << "Window Creation Failed : " << SDL_GetError() << std::endl;
pause
QuitGame();
}
else
{
std::cout << "SDL Initialized Successfully" << std::endl;
std::cout << "Renderer Initializing..." << std::endl;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
std::cout << "Renderer Creation Failed : " << SDL_GetError() << std::endl;
pause
QuitGame();
}
else
{
std::cout << "Renderer Initialized Successfully" << std::endl;
// This line (under) crashes game, and crashes visual studio...
FPS_Text_Surface = TTF_RenderText_Solid(Sans, "Frames Passed : " + FramesPassed, Color_White);
testImg = IMG_LoadTexture(renderer, "images/test.bmp");
FPS_Text = SDL_CreateTextureFromSurface(renderer, FPS_Text_Surface);
}
}
}
}
std::cout << "Game Has Successfully Initialized!" << std::endl;
return 0;
}
void InitRects()
{
sprite1_Rect.h = 32;
sprite1_Rect.w = 32;
sprite1_Rect.x = 10;
sprite1_Rect.y = 10;
FPS_Text_Rect.h = 100;
FPS_Text_Rect.w = 50;
FPS_Text_Rect.x = 2;
FPS_Text_Rect.y = 2;
}
void processInput()
{
if (SDL_PollEvent(&evnt)) {
switch (evnt.type) {
case SDL_QUIT:
QuitGame();
break;
case SDL_KEYDOWN:
switch (evnt.key.keysym.sym) {
case SDLK_a:
sprite1_Rect.x -= 1;
break;
case SDLK_d:
sprite1_Rect.x += 1;
break;
case SDLK_w:
sprite1_Rect.y-= 1;
break;
case SDLK_s:
sprite1_Rect.y += 1;
break;
}
break;
}
}
}
void QuitGame()
{
isRunning = false;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
The problem is that you have the font as a global variable and load it straight away!
You need to call TTF_Init() first and load the font after!
By using a global vairable your loading it before initializing SDL_TTF, and this way TTF_OpenFont() will return a nullptr and if you try to read a nullptr gives an Access violation reading location 0x00000000 error!
Just call TTF_OpenFont() in a function and after TTF_Init() and it will work!
Just a tip you should check that Sans isn't a nullptr before using it!
In my program I wish to be able to know if the mouse wheel has been scrolled, and if so how much in what direction. Is this possible with C++ and SFML?
So far I have this:
if (sf::Event::MouseWheelEvent().delta != 0)
{
SimulationView.zoom(1 + (10 / sf::Event::MouseWheelEvent().delta));
}
But the second line never exicutes, even when I scroll the mouse wheel
You can read the mouse wheel as part of the event loop that is polled once per frame:
int main()
{
sf::RenderWindow window(sf::VideoMode(320, 256), "Title");
sf::Event event;
while(window.isOpen())
{
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
else if(event.type == sf::Event::MouseWheelMoved)
{
// display number of ticks mouse wheel has moved
std::cout << event.mouseWheel.delta << '\n';
}
}
window.clear();
// draw window here
window.display();
}
}
Note: For SFML 2.3
Thanks to #Hiura for pointing out that sf::Event::MouseWheelMoved is deprecated in SFML 2.3.
Use this instead:
if(event.type == sf::Event::MouseWheelScrolled)
{
if(event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
std::cout << "wheel type: vertical" << std::endl;
else if(event.mouseWheelScroll.wheel == sf::Mouse::HorizontalWheel)
std::cout << "wheel type: horizontal" << std::endl;
else
std::cout << "wheel type: unknown" << std::endl;
std::cout << "wheel movement: " << event.mouseWheelScroll.delta << std::endl;
std::cout << "mouse x: " << event.mouseWheelScroll.x << std::endl;
std::cout << "mouse y: " << event.mouseWheelScroll.y << std::endl;
}
I need to write a program which programmatically changes the Landscape mode to Portrait of a touchscreen in Windows 7. The resolution is 1920 x 1080 and I thought I could switch the 1920 with the 1080 and use ChangeDisplaySettings or ChangeDisplaySettingsEx to make the change. but it doesn't work.
Windows CE has some sort of rotate feature, so I tried that. as in:
devmode.dmFields = DM_DISPLAYORIENTATION;
devmode.dmDisplayOrientation = DMDO_90;
ChangeDisplaySettingsEx(NULL,&devmode,NULL,CDS_RESET,NULL);
But that didn't work on a normal test desktop PC.
I can do this through Control Panel, Display, Screen Resolution, choose Portrait, so should I not be able to do via Windows API?
How can I make this change in code?
EDIT
I tried this code, but I get DISP_CHANGE_BADMODE returned by ChangeDisplaySettings.
#include <Windows.h>
#include <iostream>
int main() {
DEVMODE dm;
// initialize the DEVMODE structure
ZeroMemory(&dm, sizeof(dm));
dm.dmSize = sizeof(dm);
if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
{
std::cout << "Orientation\n";
switch(dm.dmDisplayOrientation) {
case DMDO_DEFAULT: std::cout << "DMDO_DEFAULT\n"; break;
case DMDO_90: std::cout << "DMDO_90\n"; break;
case DMDO_180: std::cout << "DMDO_180\n"; break;
case DMDO_270: std::cout << "DMDO_270\n"; break;
}
std::cout << "Panning height: " << dm.dmPanningHeight << '\n'
<< "Panning width: " << dm.dmPanningWidth << '\n';
std::cout << "Colour resolution: " << dm.dmBitsPerPel << " bits per pixel\n";
std::cout << "Height in pixels: " << dm.dmPelsHeight << '\n';
std::cout << "Width in pixels: " << dm.dmPelsWidth << '\n';
if(dm.dmPelsHeight > dm.dmPelsWidth) {
std::cout << "Screen should be rotated 90 degrees\n";
std::cout << "Do you want to rotate display 90 degrees? (y or n)\n";
char ch;
std::cin >> ch;
if(ch == 'y') {
// swap height and width
DWORD tmp = dm.dmPelsHeight;
dm.dmPelsHeight = dm.dmPelsWidth;
dm.dmPelsWidth = tmp;
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
// determine new orientation
switch (dm.dmDisplayOrientation)
{
case DMDO_DEFAULT:
dm.dmDisplayOrientation = DMDO_270;
break;
case DMDO_270:
dm.dmDisplayOrientation = DMDO_180;
break;
case DMDO_180:
dm.dmDisplayOrientation = DMDO_90;
break;
case DMDO_90:
dm.dmDisplayOrientation = DMDO_DEFAULT;
break;
default:
// unknown orientation value
// add exception handling here
break;
}
LONG ret = ChangeDisplaySettings(&dm, CDS_TEST); //0);
std::cout << "ChangeDisplaySettings returned " << ret << '\n';
switch(ret) {
case DISP_CHANGE_SUCCESSFUL: std::cout << "display successfully changed\n"; break;
case DISP_CHANGE_BADDUALVIEW: std::cout << "The settings change was unsuccessful because the system is DualView capable\n"; break;
case DISP_CHANGE_BADFLAGS: std::cout << "An invalid set of flags was passed in.\n"; break;
case DISP_CHANGE_BADMODE: std::cout << "The graphics mode is not supported.\n"; break;
case DISP_CHANGE_BADPARAM: std::cout << "An invalid parameter was passed in. This can include an invalid flag or combination of flags.\n"; break;
case DISP_CHANGE_FAILED: std::cout << "The display driver failed the specified graphics mode.\n"; break;
case DISP_CHANGE_NOTUPDATED: std::cout << "Unable to write settings to the registry.\n"; break;
case DISP_CHANGE_RESTART: std::cout << "The computer must be restarted for the graphics mode to work.\n"; break;
}
}
}
else
std::cout << "Screen orientation is just fine\n";
}
}
I saw an article on the web indicating that possibly it couldn't be done. But this code works.
#include <Windows.h>
#include <iostream>
int main(int argc, char* argv[]) {
DEVMODE dm;
// initialize the DEVMODE structure
ZeroMemory(&dm, sizeof(dm));
dm.dmSize = sizeof(dm);
// only change first/default display (index=0)
if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
{
std::cout << "Current orientation\n";
switch(dm.dmDisplayOrientation) {
case DMDO_DEFAULT: std::cout << "DMDO_DEFAULT\n"; break;
case DMDO_90: std::cout << "DMDO_90\n"; break;
case DMDO_180: std::cout << "DMDO_180\n"; break;
case DMDO_270: std::cout << "DMDO_270\n"; break;
}
std::cout << "Panning height: " << dm.dmPanningHeight << '\n'
<< "Panning width: " << dm.dmPanningWidth << '\n';
std::cout << "Colour resolution: " << dm.dmBitsPerPel << " bits per pixel\n";
std::cout << "Height in pixels: " << dm.dmPelsHeight << '\n';
std::cout << "Width in pixels: " << dm.dmPelsWidth << '\n';
if(argc != 2) {
std::cout << "Usage: rotate_screen <angle>. Angle values: 0, 90, 180, 270. Angles are rotation clockwise. Use 0 to revert back\n";
exit(0);
}
// parse parameter
int rotate = atoi(argv[1]); // should do something better and safer here
if(rotate != 0 && rotate != 90 && rotate != 180 && rotate != 270) {
std::cout << "incorrect rotation selected\n";
}
else {
// swap height and width
DWORD tmp = dm.dmPelsHeight;
dm.dmPelsHeight = dm.dmPelsWidth;
dm.dmPelsWidth = tmp;
// select fields which have changed
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYORIENTATION;
// set angle
switch(rotate) {
case 0: dm.dmDisplayOrientation = DMDO_DEFAULT; break;
case 90: dm.dmDisplayOrientation = DMDO_90; break;
case 180: dm.dmDisplayOrientation = DMDO_180; break;
case 270: dm.dmDisplayOrientation = DMDO_270; break;
default:
std::cout << "Something went wrong, aborting\n";
exit(0);
}
LONG ret = ChangeDisplaySettingsEx(NULL, &dm, NULL, 0, NULL); //CDS_RESET, NULL); //0);
std::cout << "ChangeDisplaySettingsEx returned " << ret << '\n';
switch(ret) {
case DISP_CHANGE_SUCCESSFUL: std::cout << "display successfully changed\n"; break;
case DISP_CHANGE_BADDUALVIEW:
std::cout << "The settings change was unsuccessful because the system is DualView capable\n";
break;
case DISP_CHANGE_BADFLAGS: std::cout << "An invalid set of flags was passed in.\n"; break;
case DISP_CHANGE_BADMODE: std::cout << "The graphics mode is not supported.\n"; break;
case DISP_CHANGE_BADPARAM: std::cout << "An invalid parameter was passed in. This can include an invalid flag or combination of flags.\n"; break;
case DISP_CHANGE_FAILED: std::cout << "The display driver failed the specified graphics mode.\n"; break;
case DISP_CHANGE_NOTUPDATED: std::cout << "Unable to write settings to the registry.\n"; break;
case DISP_CHANGE_RESTART: std::cout << "The computer must be restarted for the graphics mode to work.\n"; break;
}
}
} // enum worked
}
I'm a noob to SFML networking, i think i'm doing it all wrong, i'm constantly getting errors with this code. All i want to do is send packets of the position then update them on the other window, here is what i have so far.
#include <iostream>
#include <string>
#include "player.h"
#include "gameAssets.h"
#include "zombie.h"
using namespace std;
sf::Vector2u size(1000, 800);
bool focused = true;
sf::Vector2f newPosition, oldPosition;
//Multiplayer code.
//sf::Thread* thread = 0;
char choice;
sf::Mutex globalMutex;
sf::TcpSocket socket;
sf::IpAddress ip;
void sendandReceiveData(){
//Server half
sf::Packet packetSendX, packetSendY;
packetSendX << oldPosition.x;
packetSendY << oldPosition.y;
socket.send(packetSendX);
socket.send(packetSendY);
sf::Packet packetReceiveX, packetReceiveY;
//Client half
socket.receive(packetReceiveX);
socket.receive(packetReceiveY);
packetReceiveX >> newPosition.x;
packetReceiveY >> newPosition.y;
cout << "Other Players X: " << newPosition.x << endl;
cout << "Other Players Y: " << newPosition.y << endl;
}
int main(){
sf::RenderWindow window(sf::VideoMode(size.x, size.y), "Zombie Defence", sf::Style::Titlebar | sf::Style::Close);
//Limited fps so we dont need deltaTime...
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
player player;
zombie zombie;
gameAssets gameAssets;
player.initialize();
zombie.initialize();
gameAssets.initialize();
gameAssets.loadContent();
zombie.loadContent();
player.loadContent();
//Multiplayer code.
system("CLS");
cout << "Multiplayer Dev Build 0.1" << endl;
cout << " " << endl;
cout << "Enter (S) for server or (C) for client." << endl;
cin >> choice;
if (choice == 'S'){
sf::TcpListener listener;
listener.setBlocking(false);
listener.listen(5000);
listener.accept(socket);
cout << "New Client Connected: " << socket.getRemoteAddress() << endl;
}
else if (choice == 'C'){
cin >> ip;
if (socket.connect(ip, 5000) == sf::Socket::Done){
cout << "Connected to server" << endl;
}
}
while (window.isOpen()){
sf::View view = player.getView();
sf::Vector2f playerPosition = player.getPosition();
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed){
window.close();
}
if (event.type == sf::Event::GainedFocus){
focused = 1;
}
else if (event.type == sf::Event::LostFocus){
focused = 0;
}
}
//Only update if window is focused...
if (focused){
//Updating
player.update(window);
zombie.followPlayer(playerPosition);
zombie.update(window);
window.setView(view);
}
oldPosition = player.getPosition();
sendandReceiveData();
window.clear();
gameAssets.draw(window);
zombie.draw(window);
player.draw(window);
window.display();
}
return 0;
}
If anyone could give me a hand on what i'm doing wrong it would be appreciated, thanks.
I fixed it. All I had to do was add packetReceiveX.clear(); and the same for the y to fix the problem.