So I have programmed a simple graphical snake game using SFML in visual studio 2015 and it runs perfectly on my main computer. And I thought that I should try it on my laptop. When running the program it gave me this error: System error: The program can't start because MSVCP140D.DLL is missing from your computer. Try reinstalling the program to fix this problem So I searched it in my computer and found it so I copied it on my laptop and then again I received another error which was: Application error: The application was unable to start correctly (0xc000007b). Click OK to close the application. I tried reinstalling the Microsoft Visual C++ Redistributable and still it didn't work. (BTW it is not a code problem and I have installed SFML correctly and used its libraries and bins without any problem). Your help would mean a lot to me. Thank you! Here is my code:
//
GraphicalLoopSnakeGame.cpp :
Defines the entry point for
the console application.
//
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;
int N = 30, M = 20;
int size = 16;
int w = size*N;
int h = size*M;
int dir, num = 4;
struct Snake
{
int x, y;
} s[100];
struct Fruit
{
int x, y;
} f;
void Tick()
{
for (int i = num;i>0;--i)
{
s[i].x = s[i - 1].x;
s[i].y = s[i - 1].y;
}
if (dir == 0) s[0].y += 1;
if (dir == 1) s[0].x -= 1;
if (dir == 2) s[0].x += 1;
if (dir == 3) s[0].y -= 1;
if ((s[0].x == f.x) && (s[0].y == f.y))
{
num++; f.x = rand() % N; f.y = rand() % M;
}
if (s[0].x>N) s[0].x = 0; if (s[0].x<0) s[0].x = N;
if (s[0].y>M) s[0].y = 0; if (s[0].y<0) s[0].y = M;
for (int i = 1;i<num;i++)
if (s[0].x == s[i].x && s[0].y == s[i].y) num = i;
}
int main()
{
srand(time(0));
RenderWindow
window(VideoMode(w, h),
"Snake Game!");
Texture t1, t2, t3;
t1.loadFromFile("images/white.png");
t2.loadFromFile("images/red.png");
t3.loadFromFile("images/green.png");
Sprite sprite1(t1);
Sprite sprite2(t2);
Sprite sprite3(t3);
Clock clock;
float timer = 0, delay = 0.12;
f.x = 10;
f.y = 10;
while (window.isOpen())
{
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
}
if (Keyboard::isKeyPressed(Keyboard::Left)) dir = 1;
if (Keyboard::isKeyPressed(Keyboard::Right)) dir = 2;
if (Keyboard::isKeyPressed(Keyboard::Up)) dir = 3;
if (Keyboard::isKeyPressed(Keyboard::Down)) dir = 0;
if (timer>delay) { timer = 0; Tick(); }
////// draw ///////
window.clear();
for (int i = 0; i<N; i++)
for (int j = 0; j<M; j++)
{
sprite1.setPosition(i*size, j*size); window.draw(sprite1);
}
for (int i = 0;i<num;i++)
{
sprite2.setPosition(s[i].x*size, s[i].y*size); window.draw(sprite2);
}
sprite3.setPosition(f.x*size, f.y*size); window.draw(sprite3);
window.display();
}
return 0;
}
You are using the debug visual studio runtime, if you want to try it on another computer you should recompile your code in release mode and make sure that the appropriate visual studio runtime redistributable is installed.
If you really need to run a debug executable on another machine you need to make sure you copy the correct runtime (32 or 64-bit according to how you've compiled your program), this can be found in C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Redist\MSVC\14.24.28127\debug_nonredist
(at least for visual studio 2019, the exact path will be slightly different depending on your visual studio version, e.g. visual studio 2015 uses C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\redist\debug_nonredist
).