Why this code is skipping points while moving the mouse fast? - c

I started learning the SDL library yesterday and, after some reading and asking, made a very simple program that draws a block when the left button is down.
The problem is that it skips points when the mouse is moved fast, so you get a bunch of squares instead of a line, the following screenshot shows one line made moving the mouse with regular speed and one while moving it fast:
What is causing it to skip all those points?
Here's the code:
//keep the window open
while(running){
//handle events
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_MOUSEBUTTONDOWN:
//left button down draws black block
if(event.button.button == SDL_BUTTON_LEFT) boxColor = black;
//right button "erases" a point
else
if(event.button.button == SDL_BUTTON_RIGHT) boxColor = blue;
//middle button clears the screen
else {
clearScreen(display,blue);
break;
}
//where to draw
drawing = 1;
boxRect.x = event.button.x - BOX_WIDTH / 2;
boxRect.y = event.button.y - BOX_HEIGHT / 2;
break;
case SDL_MOUSEMOTION:
//keep drawing if the button is pressed
if(drawing == 1){
boxRect.x = event.motion.x - BOX_WIDTH / 2;
boxRect.y = event.motion.y - BOX_HEIGHT / 2;
}
break;
//stop drawing when the button is no longer pressed
case SDL_MOUSEBUTTONUP:
drawing = 0;
break;
//quit if window closing button is pressed
case SDL_QUIT:
running = 0;
break;
}
}
//draw
if(drawing == 1){
SDL_FillRect(display,&boxRect,boxColor);
SDL_Flip(display);
}
}

Because the system doesn't actually get the points as a continuous stream, it has to poll for the mouse position. This means that if you move the mouse too fast, the difference between two polls will be big enough for there to be a gap.

Related

Change the screen of LCD using button

I have two buttons first mode button and second set button connected to the 8051 microcontroller. The mode button changes the LCD screen and the set button blinks the cursor when the individual button is pressed. I am just toggling the mode button so that when the cursor is blinked the mode button performs the function of changing the screen and when the cursor is not blinked it preforms another function. But when the cursor is not blinked it's performing the function but when the cursor is blinked it changes the screen of the LCD but returns back to the initial state without pressing the button again. I want it when the cursor is blinked and the mode button is pressed to change the screen of the LCD and after reaching the last screen return to the initial first screen.
typedef int bool;
#define true 1
#define false 0
bool blink_enabled = false;
unsigned char setmode_in = 0;
static unsigned count = 0;
void display_lcd(void){
switch(key_code){
case KEY_UP:
if (setmode_in == 0){
lcd_out(0x01); //clear the screen of LCD
}else{
count = (count+1)%3;
setmode_in=0;
switch(count){
case 0:
change_screen1();
break;
case 1:
change_screen2();
break;
case 2:
change_screen3();
break;
default: break;
}
case KEY_SET:
if(blink_enabled == true){
lcd_out(0x0c); // cursor off
blink_enabled = false;
setmode_in = 0;
} else{
lcd_out(0x0F); //blink the cursor
blink_enabled = true;
setmode_in = 1;
}
break;
default: break;
}
key_code = 0;
}

Moving a square in SDL2 using C

I'm unable to move the rectangle I made in the program. There is no error message in the compiler when I run the program. Can you please tell me what I missed out in the keyboard event. Other event that I assigned to the window works fine. Thanks (an example will also be helpful).
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
SDL_Window *o;
SDL_Renderer *r;
SDL_Event e;
int i = 1;
SDL_Rect q;
SDL_Init(SDL_INIT_VIDEO);
o = SDL_CreateWindow("Game test",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
1024,
800,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
r = SDL_CreateRenderer(o, -1,SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(r,0,0,255,255);
SDL_RenderClear(r);
//Creating a box
q.x=475;
q.y=700;
q.h=50;
q.w=50;
SDL_SetRenderDrawColor(r,0,0,0,255);
SDL_RenderFillRect(r,&q);
//SDL_Delay(10);
SDL_RenderPresent(r);
while(i)
{
while(SDL_PollEvent(&e) !=0)
{
if(e.type == SDL_QUIT)
i=0;
else if(e.type == SDL_KEYDOWN)
{
switch(e.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_q:
i=0;
break;
case SDLK_UP:
q.y -=10;
SDL_Delay(11);
break;
case SDLK_DOWN:
q.y +=10;
SDL_Delay(11);
break;
case SDLK_RIGHT:
q.x +=10;
SDL_Delay(11);
break;
case SDLK_LEFT:
q.x -=10;
SDL_Delay(11);
break;
default:
break;
}
}
}
}
SDL_DestroyWindow(o);
SDL_DestroyRenderer(r);
SDL_Quit();
return 0;
}
You are only rendering the contents of the window before you enter your event loop. Since you never redraw the contents in the event loop, it's not very strange that no changes happen.
In SDL, you need to constantly redraw the window to see any changes you make. Since you only call the redraw function once, you only see what's happening in the very first moment of the window's creation. You simply need to add a redraw call inside of the loop, and it'll show you moving the rectangle as expected.

SDL_GetKeyboardState not working

I am trying to make a controller for a game with SDL 2(didn't want to ask on gamedev since it is not a game issue directly) I use SDL_GetKeyboardEvent to see if the navigation arrows are being pressed but it apparently doesn't work, it is supposed to print a value 1 or -1 if one of those keys is pressed but it doesn't it just prints 0 even if I hold the key down for several seconds, it is like it doesn't detect that the key is being pressed. I searched all over the internet and this is how they do it, but it doesn't work for me.
#define SDL_MAIN_HANDLED
#include <stdio.h>
#include "SDL.h"
/* I'll add some code later
so something that isn't used
might be initialized
*/
int main (void)
{
int a = 1;
int x;
int z;
SDL_Event quit;
const Uint8 *keys = SDL_GetKeyboardState(NULL);
SDL_Init(SDL_INIT_VIDEO);
while(a)
{
SDL_PollEvent(&quit);
if(quit.type == SDL_QUIT)
a = 0;
if(keys[SDL_SCANCODE_UP])
z = 1;
else if(keys[SDL_SCANCODE_DOWN])
z = -1;
else
z = 0;
if(keys[SDL_SCANCODE_LEFT])
x = -1;
else if(keys[SDL_SCANCODE_RIGHT])
x = 1;
else
x = 0;
printf("%d, %d\n", x, z);
//This is supposed to print
//x, z values so if up arrow is pressed it will print 0, 1 and if
//down arrow is pressed it will print 0, -1: the same with horizontal ones.
//1 or -1, 1 or -1
}
SDL_Quit();
return 0;
}
Read the documentation: wiki
Note: This function gives you the current state after all events have been processed, so if a key or button has been pressed and released before you process events, then the pressed state will never show up in the SDL_GetKeyboardState() calls
What it means is?
You need to process all events. How? Looping the PollEvent, after the loop (or if you want to check in the loop, check at the end), the SDL_GetKeyboardState is usable.
So, go through the loop, check for keyboards states. Do not forget to always go through the loop before checking for keys
e.g.
while (game)
{
/*! updates the array of keystates */
while ((SDL_PollEvent(&e)) != 0)
{
/*! request quit */
if (e.type == SDL_QUIT)
{
game = false;
}
}
if (keys[SDL_SCANCODE_RIGHT])
std::cout << "Right key";
}
You'll need to create a window with SDL_SetVideoMode to get mouse and keyboard events.
Here is my InputManager update function that updates the user input from the keyboard and mouse. Notice the const_cast needed to be able to update the class variable Uint8* keysArray;. This way, more than one event can be processed at once.
void update() {
SDL_PumpEvents();
// update keyboard state
keysArray = const_cast <Uint8*> (SDL_GetKeyboardState(NULL));
if (keysArray[SDL_SCANCODE_RETURN])
printf("MESSAGE: <RETURN> is pressed...\n");
if (keysArray[SDL_SCANCODE_RIGHT] && keysArray[SDL_SCANCODE_UP])
printf("MESSAGE: Right and Up arrows are pressed...\n");
// update mouse location and button states
SDL_GetMouseState(&x, &y);
getMouseButtonStates();
}

Set a switch on until a flag is reached and then set the switch to off

What would be the best and easiest(simplest) way to do this..?
I am writing code in C or Capel (Vector CANoe)
I have a switch on a panel which I have designed in Vector CANoe , turns a motor on and off (0 == off and 1 == on) and I want to turn the motor off when it reaches its limit .. to stop the motor burning out ! (we know it has reached its limit when the a sensor says that the limit is reached (sensor = 1 for limit reached and 0 for not reached)
How would I do this?
i want to do something like this
WHILE (clutchMotorPower==1 & sensorClutchDisengaged !=1)
break;
else clutchMotorPower==0
WHILE (clutchMotorPower==1 & sensorClutchEngaged !=1)
break;
else clutchMotorPower==0
I don't know much about the system you're working on, so this is going to be some bare-bones code. Anyway, let's go through this step-by-step.
Assumptions:
button is a variable that is 1 if the button is currently pressed and 0 otherwise.
motor is a variable that we set to 1 to turn the motor on, or 0 to turn it off.
sensor is a variable that is 1 when the sensor is activated, or 0 otherwise
First, we need code to toggle the motor state when the button is pressed. (Assume all code samples are inside a function called from your main loop)
//values of static variables are preserved between function calls
static char bActivateMotor = 0; //1 if we should activate motor, 0 otherwise
static char bButtonHeld = 0; //1 if button was pressed last loop
//(to differentiate between button being held and it being released and pressed again)
if(!button) {
bButtonHeld = 0; //if button is not being pressed, it can't be being held down.
}
if(!bActivateMotor) { //we aren't running the motor
if(button && !bButtonHeld) { //button was pressed this loop (not held from previous loop)
bButtonHeld = 1; //Don't toggle motor state next loop just because button was held down
bActivateMotor = 1; //we should be running the motor
}
else {
motor = 0;
}
}
if(bActivateMotor) { //not else because could be activated this loop
if(button && !bButtonHeld) { //button toggles motor; if we're running, stop.
bButtonHeld = 1;
bActivateMotor = 0;
}
else {
motor = 1;
}
}
Now, the next part is to stop the motor when the sensor has activated:
//values of static variables are preserved between function calls
static char bActivateMotor = 0; //1 if we should activate motor, 0 otherwise
static char bButtonHeld = 0; //1 if button was pressed last loop
//(to differentiate between button being held and it being released and pressed again)
if(!button) {
bButtonHeld = 0; //if button is not being pressed, it can't be being held down.
}
if(!bActivateMotor) { //we aren't running the motor
if(button && !bButtonHeld) { //button was pressed this loop (not held from previous loop)
bButtonHeld = 1; //Don't toggle motor state next loop just because button was held down
bActivateMotor = 1; //we should be running the motor
}
else {
motor = 0;
}
}
if(bActivateMotor) { //not else because could be activated this loop
if(button && !bButtonHeld) { //button toggles motor; if we're running, stop.
bButtonHeld = 1;
bActivateMotor = 0;
}
/////////////////////
// Added Code Here //
/////////////////////
else if(sensor) {
bActivateMotor = 0; //motor will shut off next loop
}
else {
motor = 1;
}
}
With the lack of specifics about your code, it's hard to figure out exactly what difficulties you might be having, but hopefully the algorithm above will be a good starting point.

XNA/C# projectiles firing issue - need one projectile at a time to fire on spacebar press

Problem: Upon spacebar press i need my projectile to fire and continue its life regardless of another press or letting go of the spacebar.
below is both the code adding, updating, and drawing the projectiles. I have tried numerous reworkings of this code but alas am not experienced enough to have found a suitable solution yet.
AddProjectile code below
enter code here
//add projectile if spacebar is pressed
private void AddProjectile(Vector2 position)
{
//i need somthing like this to make bullets autofire on phone
//while (TouchPanel.IsGestureAvailable)
//{
// Projectile projectile = new Projectile();
// projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position);
// projectiles.Add(projectile);
//}
if (currentKeyboardState.IsKeyDown(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Pressed)
{
Projectile projectile = new Projectile();
projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position);
projectiles.Add(projectile);
}
}
Update Projectile code below
private void UpdateProjectiles()
{
//update projectiles
for (int i = projectiles.Count - 1; i >= 0; i--)
{
if (currentKeyboardState.IsKeyDown(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Pressed)
{
//adds particle to first projectile but not again until the next fire butotn press
//particleEngine.EmitterLocation = new Vector2(projectiles[i].Position.X, projectiles[i].Position.Y);
projectiles[i].Update();
projectileOn = true;
}
//if projectiles not being fired remove them from the game screen
else
{
projectiles.RemoveAt(i);
}
}
}
the Draw method that draws projectiles to screen
//draw the projectiles
//***********************************************************
//using the if here allows control of projectiles to pass...
//...to the "currentstate of the spacebar (is pressed = fire)
/***/
if (currentKeyboardState.IsKeyDown(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Pressed)
{
for (int i = 0; i < projectiles.Count; i++)
{
projectiles[i].Draw(spriteBatch);
}
}
//remove projectiles and reset
if (currentKeyboardState.IsKeyUp(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Released)
{
UpdateProjectiles();
}
So, this is what i have so far and as stated can get the projectile to worl fine , i just cant get them to continue their life (until a collision or they reach the end of screen) once i let go of the keyboard spacebar.
A quick overview of the problems I see that are causing your isses:
You're checking if keys/buttons are pressed during your add, update, and draw methods.
If a key isn't pressed, you're removing the item during an update and not drawing it during a draw.
To accomplish what I think you're trying to accomplish, I'd do something along these lines (tailored and cleaned up for your needs, of course):
private void Update(GameTime gameTime)
{
// Only add a new projectile if Space or A is pressed
if (currentKeyboardState.IsKeyDown(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Pressed)
{
AddProjectile(); // some method that adds a new projectile to 'projectiles'
}
// Update all existing projectiles, regardless of button press.
// This will allow your projectiles to continue flying after they have been fired.
for (int i = 0; i < projectiles.Count; i++)
{
projectiles[i].Update();
if (projectiles[i].Dead) // check if projectiles[i] is out of bounds or has collided with something
{
projectiles.RemoveAt(i);
i--;
}
}
}
private void Draw(GameTime gameTime)
{
// Draw all existing projectiles, regardless of button press.
for (int i = 0; i < projectiles.Count; i++)
{
projectiles[i].Draw(spriteBatch);
}
}
Your space bar or A button is only used to fire a new projectile. You want that projectile to fly until it hits something or flies off the side of the screen, and it shouldn't depend on whether you have a button pressed in order to do that. Neither should drawing the bullet.
Make sense?

Resources