#include <SDL2/SDL.h> #include <SDL2/SDL_image.h> #include <iostream> #include <cmath> const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int FPS = 60; const double PI = 3.141592653589793; // Structure to store the position and velocity of an object struct Vector2D { double x, y; double dx, dy; }; // Calculate the distance between two vectors double distance(Vector2D v1, Vector2D v2) { return sqrt(pow(v1.x - v2.x, 2) + pow(v1.y - v2.y, 2)); } int main(int argc, char* argv[]) { // Initialize SDL SDL_Init(SDL_INIT_VIDEO); // Create a window and renderer SDL_Window* window = SDL_CreateWindow("Earth Animation", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); // Load the Earth texture SDL_Surface* surface = IMG_Load("earth.png"); SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_FreeSurface(surface); // Create a vector for the Earth's position and velocity Vector2D earth; earth.x = SCREEN_WIDTH / 2; earth.y = SCREEN_HEIGHT / 2; earth.dx = 0; earth.dy = -200; // Create a vector for the Moon's position and velocity Vector2D moon; moon.x = SCREEN_WIDTH / 2; moon.y = SCREEN_HEIGHT / 2 - 200; moon.dx = 0; moon.dy = -250; // Set up the game loop bool quit = false; SDL_Event event; Uint32 startTime, endTime, deltaTime, delayTime; startTime = SDL_GetTicks(); while (!quit) { // Handle events while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { quit = true; } } // Calculate the deltaTime endTime = SDL_GetTicks(); deltaTime = endTime - startTime; // Move the Earth and Moon earth.x += earth.dx * (deltaTime / 1000.0); earth.y += earth.dy * (deltaTime / 1000.0); moon.x += moon.dx * (deltaTime / 1000.0); moon.y += moon.dy * (deltaTime / 1000.0); // Calculate the angle between the Earth and Moon double angle = atan2(earth.y - moon.y, earth.x - moon.x); // Move the Moon in a circular orbit around the Earth moon.x = earth.x + cos(angle) * distance(earth, moon); moon.y = earth.y + sin(angle) * distance(earth, moon); // Clear the renderer SDL_RenderClear(renderer); // Render the Earth and Moon SDL_Rect earthRect = { (int)(earth.x - 64), (int)(earth.y - 64), 128, 128 }; SDL_Rect moonRect = { (int)(moon.x - 32), (int)(moon.y - 32), 64, 64 }; SDL_RenderCopy(renderer, texture, NULL, &earthRect); SDL_RenderCopy(renderer, texture, NULL, &moon
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}