I am making a solar system simulator in Processing. I have initialsed an array in the Planet class which holdes the textures for the PShape planetary objects.
I have added this array to the constructor. Then I tried to call it in the main class where I have initialised the planets, but I get the following erros:
Main class: Syntax Error - Error on parametre or method declaration near 'void'?
Clearly I am missing something or done something wrong. Thank you for the reply.
// Solar_system_enginee main class
//Solar system and it's gravitation's simulation.
// The central sun applies gravitation to other celestial bodies
PImage background;
PImage sun_Texture;
float cameraX, cameraY;
// zoom and pan
float scale = 0.06;
float xPan = 600;
float yPan = 398;
boolean zoomIn = false;
boolean zoomOut = false;
//includes
Sun sun;
Planet mercury;
Planet venus;
Planet earth;
Planet mars;
Planet jupiter;
Planet saturn;
Planet uranus;
Planet neptune;
Planet pluto;
Planet _planetTextures
//screen setup
void setup() {
//Screen size
size(1200, 799, P3D);
sun_Texture = loadImage("sun.jpg");
// Celestial object properties
// 1; Planet's size
// 2; Planet's color / texture
// 3; Movement speed
// 4; Planet's path color
// 5; Planet's path size
// 6; Distance from Sun
// 7; Planet's number of moons // need to check it
//Spawn celestial objects
sun = new Sun(696, color(255, 50, 0));
mercury = new Planet(5, 0.2, 2, color(255, 200, 100), 1448, _planetTextures[0]);
venus = new Planet(12, 0.2, 2, color(150, 10, 100), 1499, _planetTextures[1]);
earth = new Planet(13, 0.2, 2, color(255, 255, 100, 100), 1540, _planetTextures[2]);
//mars = new Planet(7, 0.5, 2, color(167, 45, 160), 1618);
//jupiter = new Planet(143, 0.5, 2, color(167, 45, 160), 2169);
//saturn = new Planet(121, 0.5, 2, color(167, 45, 160), 2825);
//uranus = new Planet(51, 0.5, 2, color(167, 45, 160), 4263);
//neptune = new Planet(50,0.5, 2, color(167, 45, 160), 5886);
//pluto = new Planet(2, 0.5, 2, color(167, 45, 160), 6486);
}
// sceene setup
void sceene() {
// Load background image
background = loadImage("stars.jpg");
background(background);
// Set it in the middle of the screen
translate(width/2, height/2, 0);
}
// draw setup
void draw() {
// Zoom towards the center of the screen, rather the edge of the screen
translate(width/2, height/2, 0);
scale(scale);
// Translate Y and X pan
translate( -xPan, -yPan);
// Draw background
sceene();
// Zoom In
if (zoomIn) {
scale *= 1.01;
}
// Zoom Out
if (zoomOut) {
scale /= 1.01;
}
// Display sun and rotate Y axis camera around sun
sun.display();
sun.move(mouseY);
//mercury
mercury.display();
mercury.move();
//venus
venus.display();
venus.move();
//earth
earth.display();
earth.move();
////mars
//mars.display();
//mars.move();
////jupiter
//jupiter.display();
//jupiter.move();
////saturn
//saturn.display();
//saturn.move();
////uranus
//uranus.display();
//uranus.move();
////neptune
//neptune.display();
//neptune.move();
////pluto
//pluto.display();
//pluto.move();
}
// Key press record function, responsible for zoom in and out of scene
void keyPressed() {
if (keyCode == UP) {
zoomIn = true;
zoomOut = false;
}
if (keyCode == DOWN) {
zoomIn = false;
zoomOut = true;
}
}
void keyReleased() {
if (keyCode == UP) {
zoomIn = false;
}
if (keyCode == DOWN) {
zoomOut = false;
}
}
// Planets class
class Planet {
float radius = 0;
float moveSpeed;
float distance;
float size;
color planetColor;
int pathSize;
color pathColor;
PShape planet;
PImage[] planetTextures = new PImage [3];
Moon moon;
Planet(int _size, float _speed, int _path_size, color _path_color, float _dist, PImage[] _planetTextures) { //color _color
size = _size;
//planetColor = _color;
moveSpeed = _speed;
pathColor = _path_color;
pathSize = _path_size;
distance = _dist;
planetTextures = _planetTextures;
moon = new Moon(10, color(200, 200, 100));
// Set planet's texture
noStroke();
noFill();
planet = createShape(SPHERE, size);
planet.setTexture(_planetTextures[0]);
// Load textures into an array
planetTextures[0] = loadImage ("mercury.jpg");
planetTextures[1] = loadImage ("venus.jpg");
planetTextures[2] = loadImage ("earth.jpg");
}
void display() {
pushMatrix();
//path
stroke(pathColor);
strokeWeight(2);
noFill();
rotateY(radius * moveSpeed);
rotateX(PI/2);
ellipse(0, 0, 2*distance, 2*distance);
//body
noStroke();
fill(planetColor);
translate(distance, 0, 0);
shape(planet);
//sphere(size);
moon.display();
moon.move();
popMatrix();
}
void move() {
radius += moveSpeed;
}
}
The Syntax Error - Missing name or ; near ‘void’? error occurs between Planet _planetTextures and void setup().
Sometimes error can be a bit cryptic: in this case it's as straightforward as it can be: you forgot to add ; at the end of Planet _planetTextures.
That being said, I've spotted a bunch of other areas in your code you might want to revise:
_planetTextures is later used as if it's a PImage[] in setup() when you instantiate planets
the Planet constructor takes in a PImage[] _planetTextures argument. If a single planet uses a single texture, maybe you meant PImage _planetTexture instead ?
The Planet constructor loads 3 images (mercury, venus, earth): should it be task of the main sketch to load all assets, then pass each single PImage to it's respective planet (instead of reloading the same 3 images for every single planet instance)
since the planet texture is only once in the constructor, there's no need for the PImage[] property
the backround PImage was loaded continously, multiple times per frame in sceene() (called from draw()): you might want to load the image once in setup.
Here is your code with the above tweaks applied:
//Solar system and it's gravitation's simulation.
// The central sun applies gravitation to other celestial bodies
PImage background;
PImage sun_Texture;
float cameraX, cameraY;
// zoom and pan
float scale = 0.06;
float xPan = 600;
float yPan = 398;
boolean zoomIn = false;
boolean zoomOut = false;
//includes
Sun sun;
Planet mercury;
Planet venus;
Planet earth;
Planet mars;
Planet jupiter;
Planet saturn;
Planet uranus;
Planet neptune;
Planet pluto;
PImage[] planetTextures;
//screen setup
void setup() {
//Screen size
size(1200, 799, P3D);
sun_Texture = loadImage("sun.jpg");
// Celestial object properties
// 1; Planet's size
// 2; Planet's color / texture
// 3; Movement speed
// 4; Planet's path color
// 5; Planet's path size
// 6; Distance from Sun
// 7; Planet's number of moons // need to check it
// Load background image
background = loadImage("stars.jpg");
// Load textures into an array
planetTextures[0] = loadImage ("mercury.jpg");
planetTextures[1] = loadImage ("venus.jpg");
planetTextures[2] = loadImage ("earth.jpg");
//Spawn celestial objects
sun = new Sun(696, color(255, 50, 0));
mercury = new Planet(5, 0.2, 2, color(255, 200, 100), 1448, planetTextures[0]);
venus = new Planet(12, 0.2, 2, color(150, 10, 100), 1499, planetTextures[1]);
earth = new Planet(13, 0.2, 2, color(255, 255, 100, 100), 1540, planetTextures[2]);
//mars = new Planet(7, 0.5, 2, color(167, 45, 160), 1618);
//jupiter = new Planet(143, 0.5, 2, color(167, 45, 160), 2169);
//saturn = new Planet(121, 0.5, 2, color(167, 45, 160), 2825);
//uranus = new Planet(51, 0.5, 2, color(167, 45, 160), 4263);
//neptune = new Planet(50,0.5, 2, color(167, 45, 160), 5886);
//pluto = new Planet(2, 0.5, 2, color(167, 45, 160), 6486);
}
// sceene setup
void scene() {
background(background);
// Set it in the middle of the screen
translate(width/2, height/2, 0);
}
// draw setup
void draw() {
// Zoom towards the center of the screen, rather the edge of the screen
translate(width/2, height/2, 0);
scale(scale);
// Translate Y and X pan
translate( -xPan, -yPan);
// Draw background
scene();
// Zoom In
if (zoomIn) {
scale *= 1.01;
}
// Zoom Out
if (zoomOut) {
scale /= 1.01;
}
// Display sun and rotate Y axis camera around sun
sun.display();
sun.move(mouseY);
//mercury
mercury.display();
mercury.move();
//venus
venus.display();
venus.move();
//earth
earth.display();
earth.move();
////mars
//mars.display();
//mars.move();
////jupiter
//jupiter.display();
//jupiter.move();
////saturn
//saturn.display();
//saturn.move();
////uranus
//uranus.display();
//uranus.move();
////neptune
//neptune.display();
//neptune.move();
////pluto
//pluto.display();
//pluto.move();
}
// Key press record function, responsible for zoom in and out of scene
void keyPressed() {
if (keyCode == UP) {
zoomIn = true;
zoomOut = false;
}
if (keyCode == DOWN) {
zoomIn = false;
zoomOut = true;
}
}
void keyReleased() {
if (keyCode == UP) {
zoomIn = false;
}
if (keyCode == DOWN) {
zoomOut = false;
}
}
class Planet {
float radius = 0;
float moveSpeed;
float distance;
float size;
color planetColor;
int pathSize;
color pathColor;
PShape planet;
Moon moon;
Planet(int _size, float _speed, int _path_size, color _path_color, float _dist, PImage _planetTexture) { //color _color
size = _size;
//planetColor = _color;
moveSpeed = _speed;
pathColor = _path_color;
pathSize = _path_size;
distance = _dist;
moon = new Moon(10, color(200, 200, 100));
// Set planet's texture
noStroke();
noFill();
planet = createShape(SPHERE, size);
planet.setTexture(_planetTexture);
}
void display() {
pushMatrix();
//path
stroke(pathColor);
strokeWeight(2);
noFill();
rotateY(radius * moveSpeed);
rotateX(PI/2);
ellipse(0, 0, 2*distance, 2*distance);
//body
noStroke();
fill(planetColor);
translate(distance, 0, 0);
shape(planet);
//sphere(size);
moon.display();
moon.move();
popMatrix();
}
void move() {
radius += moveSpeed;
}
}
It would've made it easier to for others to test if you would have also posted the Sun/Moon classes and the textures uses: something to remember for future posts. (The easier it is for others to replicate your issue the more like to get (good) answers).
Not bad progress and you're getting the main points about classes.
There might still be confusion over how arguments get passed from the sketch(global) scope to instances, but that may also be an artefact of perhaps trying to write a lot of code in one go ?
If that's the case, I recommend slowing down, writing one bit of functionality at a time (e.g. one function or class method at a time, running the sketch and testing to ensure it works as expected first). Once a bit of code works as expected to you can move to the next and ideally text combinations of the newly added functionalities to ensure that there no weird interactions between classes. Constantly testing how the code behaves may appear slower, but resting assured the code works as go along is faster on the long run and it definitely makes debugging much easier (likely to be most recently added functionality which is also freshes in your memory). Have fun coding !
I try to redraw window only when is need, but when window is resize in a hop (for example maximize), SDL_Renderer is not usable in new size some time after action.
Here is my simple example code:
#include <SDL2/SDL.h>
bool redraw = true;
void draw(SDL_Renderer* renderer, SDL_Rect& rect)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 50, 0xFF);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 90, 90, 90, 0xFF);
SDL_RenderDrawLine(renderer, 0, 0, rect.w, rect.h);
SDL_RenderPresent(renderer);
redraw = false;
}
SDL_Rect resize(const SDL_Event& event)
{
int win_w = (int) event.window.data1;
int win_h = (int) event.window.data2;
SDL_Rect rect = {0, 0, win_w, win_h};
redraw = true;
return rect;
}
int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("Maximize test",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800, 450, 0);
SDL_SetWindowResizable(window, SDL_TRUE);
SDL_Renderer* renderer = SDL_CreateRenderer(
window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
bool running = true;
SDL_Event event;
SDL_Rect rect = {0, 0, 800, 450};
while (running) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
rect = resize(event);
break;
}
break;
}
if (redraw) {
draw(renderer, rect);
}
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
return 0;
}
At first, I try to get new renderer size SDL_GetRendererOutputSize in same result like getting size from event.
When I try to resize window with typical mouse motion actions, it works fine. But maximize or window manager resizing to one screen side does not work.
I try to use different event like SDL_WINDOWEVENT_SIZE_CHANGED, but with same result.
And yes, normally, I checks all SDL calls, with SDL_RenderDrawLine but without any errors.
I have recently started learning game programming in C. I am using a virtual studio code on linux Ubuntu, and image star.png is not displaying on the screen, there are no errors or problems reported by vs code. I have tried reinstalling sdl2-dev and sdl2-image-dev libraries. Maybe it is a problem with SDL, or maybe with my code (it was written in 2013).
The code should draw a white ractangl, which i can move around, on a blue screen and place a star in upper left corner of a window. It does everything except placing a star.
The code: (doesn't show "Cannot dinf star.png!" so I guess it is not that)
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
typedef struct
{
int x, y;
short life;
char *name;
}Man;
typedef struct
{
//players
Man man;
//image
SDL_Texture *star;
} GameState;
/////////////////////////////////////////////////////////////////////processEvents
int processEvents(SDL_Window *window , GameState *game)
{
SDL_Event event;
int done = 0;
//Check for events
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_WINDOWEVENT_CLOSE:
{
if (window)
{
SDL_DestroyWindow(window);
window = NULL;
done = 1;
}
}
break;
case SDL_KEYDOWN:
{
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
done = 1;
break;
}
}
break;
case SDL_QUIT:
done = 1;
break;
}
}
const Uint8 *state = SDL_GetKeyboardState(NULL);
if(state[SDL_SCANCODE_LEFT])
{
game->man.x -= 1;
}
if(state[SDL_SCANCODE_RIGHT])
{
game->man.x += 1;
}
if(state[SDL_SCANCODE_UP])
{
game->man.y -= 1;
}
if(state[SDL_SCANCODE_DOWN])
{
game->man.y += 1;
}
SDL_Delay(10);
return done;
}
/////////////////////////////////////////////////////////////////////////////////////doRender
void doRedner(SDL_Renderer *renderer, GameState *game)
{
SDL_SetRenderDrawColor(renderer , 0, 0, 255, 255); //blue
//screen clean up into blue
SDL_RenderClear(renderer);
//set color
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect rect = {game->man.x, game->man.y, 80, 80};
SDL_RenderFillRect(renderer, &rect);
//draw a star
SDL_Rect starRect = { 50, 50, 64, 64 };
SDL_RenderCopy(renderer, game->star, NULL, &starRect);
SDL_RenderPresent(renderer);
}
/////////////////////////////////////////////////////////////////////////////////////main funkction
int main(int argc, char *argv[])
{
GameState gameState;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Surface *starSurface = NULL;
SDL_Init(SDL_INIT_VIDEO);
gameState.man.x = 340-40;
gameState.man.y = 240-40;
window = SDL_CreateWindow("Game Widnow",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
0
);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
//load image
starSurface = IMG_Load("star.png");
if(starSurface = NULL)
{
printf("Cannot find star.png!\n\n");
SDL_Quit();
return 1;
}
gameState.star = SDL_CreateTextureFromSurface(renderer, starSurface);
SDL_FreeSurface(starSurface);
int done = 0;
while(!done)
{
//check events
done = processEvents(window, &gameState);
//render
doRedner(renderer, &gameState);
}
SDL_Delay(1000);
//exit game and free memory
SDL_DestroyTexture(gameState.star);
//destroy and close window
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
return 0;
}
TL;DR: Your intention is to perform a comparison, but you are using the assignment operator (i.e., =) instead of the comparison operator ==.
The conditional expression in your if statement:
if(starSurface = NULL)
{
...
}
does not check whether starSurface is NULL. The expression starSurface = NULL assigns NULL to starSurface and evaluates to NULL. Therefore, the condition evaluates to false and no error message is displayed.
Instead, you should write (note the double = below):
if (starSurface == NULL)
or just:
if (!starSurface)
I'm programming in c4droid but I can't get the touch event to work.
Tried switch or if statement but nothing works im sure rendering is ok because if I delete the switch then its renders normally
Here I the code:
#include <SDL2/SDL.h>
int main()
{
SDL_Window *window = NULL;
window = SDL_CreateWindow("Shooter", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
// Setup renderer
SDL_Renderer *renderer = NULL;
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED);
// Set render color to red ( background will be rendered in this color )
SDL_SetRenderDrawColor(renderer, 0, 255, 25, 255);
// Clear winow
SDL_RenderClear(renderer);
// Creat a rect at pos ( 50, 50 ) that's 50 pixels wide and 50 pixels
// high.
SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255);
SDL_Rect r;
r.x = 500;
r.y = 500;
r.w = 50;
r.h = 50;
SDL_Rect e;
e.x = 5;
e.y = 5;
e.w = 50;
e.h = 50;
SDL_Event event;
while(SDL_PollEvent(&event) != 0) {
switch (event.type) {
case SDL_FINGERDOWN :
e.x = e.x + 10;
SDL_RenderFillRect(renderer, &r);
SDL_RenderFillRect(renderer, &e);
SDL_RenderPresent(renderer);
break;
}
}
// Wait for 5 sec
SDL_Delay(50000);
SDL_DestroyWindow(window);
SDL_Quit();
}
You need to make sure to poll the event.
while(true) {
while(SDL_PollEvent(&event) != 0) {
switch (event.type) {
case SDL_FINGERDOWN :
e.x = e.x + 10;
SDL_RenderFillRect(renderer, &r);
SDL_RenderFillRect(renderer, &e);
SDL_RenderPresent(renderer);
break;
// Render rect
}
}
}
Provided event is not NULL, SDL_PollEvent will take the next event from the queue and store it in the SDL_Event that event is pointing at.
Edit: Don't remove the while(true) loop, put this one inside it. Sorry, I probably should have been a bit more clear in the beginning.
I'm using Pebble SDK 2 and running into graphics problems.
I'm trying to add a full screen graphic with the dimensions 144 x 168, however, when I do-- the bottom gets clipped.
Investigating further- the root layer has the dimensions of 144 x 152 rather than 144 x 168 (Pebble's fullscreen dimensions). I set the window to be full screen, before adding the bitmap and calling window_stack_push, so the status bar should not be an issue (although, the dimensions of the status bar supposedly fit the space I am missing).
Code is below:
void handle_init(void) {
s_main_window = window_create();
window_set_fullscreen(s_main_window, true);
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload
});
const bool animated = true;
window_set_click_config_provider(s_main_window, click_config_provider);
window_stack_push(s_main_window, animated);
}
static void main_window_load(Window *window) {
// Create GBitmap, then set to created BitmapLayer
s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_CHILL_BG5);
s_background_layer = bitmap_layer_create(GRect(0, 0, 144, 168));
bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap);
layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(s_background_layer));
}
Updated Code:
static void breathe_window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
// Create GBitmap, then set to created BitmapLayer
s_background_bitmap_3 = gbitmap_create_with_resource(RESOURCE_ID_CHILL_BG6);
s_background_layer_3 = bitmap_layer_create(GRect(0, -16, 144, 168));
bitmap_layer_set_bitmap(s_background_layer_3, s_background_bitmap_3);
layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(s_background_layer_3));
}
static void breathe_window_unload(Window *window) {
gbitmap_destroy(s_background_bitmap_3);
bitmap_layer_destroy(s_background_layer_3);
animation_unschedule_all();
window_destroy(s_window_2);
}
static void up_click_handler(ClickRecognizerRef recognizer, void *context) {
s_window_2 = window_create();
window_set_window_handlers(s_window_2, (WindowHandlers) {
.load = breathe_window_load,
.unload = breathe_window_unload
});
const bool animated = true;
window_stack_push(s_window_2, animated);
next_animation();
}